I am trying to use unit test my constructor. Inside the constructor, I add a custom service that stores data and processes HTTP requests. in my function to save my constructor, I create an object in the scope by calling the constructor for my user service.
I want to test to make sure the constructor of my custom service is called. I'm trying to use a jasmine spy to spy on a constructor call, but to no avail. I am trying to follow the example of Jasmine on how to execute a constructor spy ([Jasmine Spies]) 1 , but this is not work.
My controller is defined as follows:
controller('comments.EditCtrl', ['$scope', '$location', '$routeParams', 'Comment', function($scope, $location, $routeParams, Comment) { $scope.save = function(comment) { $scope.comment = new Comment(comment); $scope.comment.postId = $routeParams.postId; Comment.save($scope.comment, function() { $location.path('/blog/' + $routeParams.postId); }); }; }])
The custom service is called Comment. Line of interest
$scope.comment = new Comment(comment);
I can not check if this works correctly. My test code for the controller is as follows:
describe('comment.edit', function() { beforeEach(function() { module('app'); module('blog.comments'); module('comments.edit'); }); describe('edit controller', function() { var $scope, editCtrl, Comment, commentNamespace; var mockComment = { email: ' spencerdev@maasive.net ', text: 'mock text' }; beforeEach(inject(function($rootScope, $injector, $controller) { var routeParamsStub = jasmine.createSpy('routeParamsStub'); routeParamsStub.postId = '7'; Comment = $injector.get('Comment'); commentNamespace = { Comment: Comment }; commentNamespace.Comment.save = function(comment, callback) { callback(); return ''; }; $scope = $rootScope.$new(); editCtrl = $controller('comments.EditCtrl', { $scope: $scope, $routeParams: routeParamsStub, Comment: commentNamespace.Comment }); })); it('should have a edit controller', function() { expect(editCtrl).not.toBe(null); expect(editCtrl).not.toBe(undefined); }); describe('save function', function() { beforeEach(function() { spyOn(commentNamespace, 'Comment'); $scope.save(mockComment); });
You can see that I'm trying to create a namespace, as Jasmine suggests, and then spy on the “comment” so that, I hope, picks up a constructor call. When I run the test, I get the following error message from Karma:
[2013-07-23 21:58:47.720] [DEBUG] config - autoWatch set to false, because of singleRun INFO [karma]: Karma server started at http://localhost:8080/ INFO [launcher]: Starting browser Chrome INFO [Chrome 28.0 (Mac)]: Connected on socket id QmSDHIAEJnuGEbe8Zmq3 Chrome 28.0 (Mac) LOG: null Chrome 28.0 (Mac) comment.edit edit controller save function should create a Comment object via its constructor FAILED Expected spy constructor to have been called. Error: Expected spy constructor to have been called. at null.<anonymous> (/Users/spencer/Projects/angular-blog/src/app/blog/comments/edit/edit.unit.js:68:55) Chrome 28.0 (Mac): Executed 20 of 20 (1 FAILED) (0.266 secs / 0.092 secs) Warning: Task "karma:unit" failed. Use
I feel like I tried every combination of other tactics like spying on Comment.prototype.constructor, but I get the same message every time. Does anyone know how to do this? I know there are a lot of things here, let me know if there are any holes in the information where I may have left something. Thanks