I am currently facing a challenge while attempting to develop unit tests for my application.
Within my controller, I have the following code snippet:
$scope.test1 = function() {
productFactory.getName()
.then(function(products){
$scope.result = products;
})
}
The 'productFactory' section of my code is defined as:
angular.module('myApp').factory('productFactory', function($http) {
var factoryObj = {};
factoryObj.getName = function() {
return http.get(url)
}
return factoryObj
})
In my unit test file, I have:
describe('test here', function () {
var testCtrl, scope, httpBackend, mockFactory;
beforeEach(module('myApp', function($provide){
$provide.value('productFactory', mockFactory);
}));
// Initialize the controller and a mock scope
beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, _productFactory_) {
scope = _$rootScope_.$new();
httpBackend = _$httpBackend_;
mockFactory = _productFactory_;
testCtrl = _$controller_('testCtrl', {
$scope: scope
});
it('should get product name', function() {
scope.test1();
//I am not sure how to test the results
});
}));
During the execution of karma test, an error occurs:
TypeError: 'undefined' is not an object (evaluating 'productFactory.getName()')
I am seeking guidance on how to effectively test the http result and resolve this issue. Any assistance would be greatly appreciated. Thank you!