AngularJS + Jasmine how to spy on a service's constructor called in a controller -
i trying unit test constructor of mine. within constructor, injecting custom service houses data , makes http requests. within save function of constructor, instantiate object on scope calling constructor custom service.
i want test make sure constructor of custom service being called. trying use jasmine spy spy on constructor call, unsuccessfully. trying follow example given jasmine on how perform spy on constructor ([jasmine spies])1, not working.
my controller defined 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 named 'comment'. line of interest is
$scope.comment = new comment(comment);
i can't test work properly. test code controller 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 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); }); //todo: figure out how spy on comment constructor call it('should create comment object via constructor', function() { expect(commentnamespace.comment).tohavebeencalled(); }); }); }); });
you can see trying create namespace jasmine suggests, , spy on 'comment' pick constructor call. when run test, following error message karma:
[2013-07-23 21:58:47.720] [debug] config - autowatch set false, because of singlerun info [karma]: karma server started @ 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 comment object via constructor failed expected spy constructor have been called. error: expected spy constructor have been called. @ 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 --force continue. aborted due warnings.
i feel have tried every combination of other tactics spying on comment.prototype.constructor, same message every time. have clue on how this? know there lot here, let me know if there holes in info may have left out. thanks
this due how jasmine installs spies. when spyon(foo, 'bar')
jasmine replaces bar
attribute on foo
spy object behaves method. means when pass off commentnamespace.comment
controller, has reference original constructor, when spyon(commentnamespace, 'comment')
later, it's late.
Comments
Post a Comment