I'm currently working on creating unit tests for my controller.
Here's a snippet of what I have:
angular.module('myApp').controller('testCtrl', ['$scope', 'testFactory',
function($scope, testFactory) {
//I'm encountering an error when running Karma
//TypeError: 'undefined' is not a function (evaluating
//'$scope.$watch')
$scope.$watch(function(){
return testFactory.returnItem; // watching the data returned by the factory
}, function(newVal){
console.log('call here')
});
}
]}
In my factory file:
angular.module('myApp').factory('testFactory', ['Product','$cookies','$q',
function(Product ,$cookies, $q) {
var service = {};
service.getProduct = function() {
var deferred = $q.defer();
var that = this;
Product.query({
Id: 123,
}, function(product) {
that.returnItem = product;
deferred.resolve(product);
});
return deferred.promise;
};
return service;
}
]);
For my unit tests:
describe('Test ', function () {
beforeEach(module('myApp'));
var $controller, testFactory;
// Setting up the controller and mock scope
beforeEach(inject(function(_$controller_, _testFactory_){
$controller = _$controller_;
testFactory = _testFactory_;
}));
describe('Initial test', function() {
it('should return data', function() {
var $scope = {};
var controller = $controller('testCtrl', {$scope:$scope});
//not sure what to do next….
});
});
})
I've hit a roadblock with the error message and I'm unsure how to proceed with testing the getProduct
method in my service from the controller.