angularjs - How to invoke spyOn on a scope function -
i have following jasmine spec.
describe('viewmeetingctrl', function () { var $rootscope, scope, $controller , $q ; beforeeach(angular.mock.module('myapp')); beforeeach(inject(function ($rootscope, $controller ) { scope = $rootscope.$new(); createcontroller = function() { return $controller('viewmeetingctrl', { $scope: scope, meeting : {} }); }; })); it('the meeting type should equal object', function () { var controller = new createcontroller(); //some assertion }); });
following viewmeetingctrl.js
(function () { 'use strict'; angular.module('myapp').controller('viewmeetingctrl', viewmeetingctrl); viewmeetingctrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationservice', 'meetingservice', '$modal', 'meeting', 'attachmentservice']; function viewmeetingctrl($scope, $state, $http, $translate, notificationservice, meetingservice, $modal, meeting, attachmentservice) { $scope.meeting = meeting; $scope.cancelmeeting = cancelmeeting; function cancelmeeting(meetingid, companyid) { meetingservice.sendcancelnotices(companyid, meetingid) .success(function () { $state.go('company.view'); }); } //more code } })();
my question how invoke spyon (or other jasmine spies related method) method on above cancelmeeting() can mock method calls , returns etc. did following
describe('viewmeetingctrl', function () { var $rootscope, scope, $controller , $q ; beforeeach(angular.mock.module('myapp')); beforeeach(inject(function ($rootscope, $controller ) { scope = $rootscope.$new(); createcontroller = function() { return $controller('viewmeetingctrl', { $scope: scope, meeting : {} }); }; })); it('the meeting type should equal object', function () { spyon(scope, 'cancelmeeting');//cancelmeeting inside scope did var controller = new createcontroller(); }); });
but following output
firefox 37.0.0 (windows 8.1) viewmeetingctrl meeting type should equal object failed error: cancelmeeting() method not exist in c:/users/work/myapp/tests/node_mo dules/jasmine-core/lib/jasmine-core/jasmine.js (line 1895)
is way invoking spyon wrong or syntax's missing ?. or missing fundamental here ?
the cancelmeeting
function not added scope until controller created. think need reverse lines in test code:
it('the meeting type should equal object', function () { var controller = new createcontroller(); spyon(scope, 'cancelmeeting'); });
Comments
Post a Comment