Angularjs 如何在作用域函数上调用spyOn
我有以下茉莉花规格Angularjs 如何在作用域函数上调用spyOn,angularjs,unit-testing,jasmine,karma-runner,karma-jasmine,Angularjs,Unit Testing,Jasmine,Karma Runner,Karma Jasmine,我有以下茉莉花规格 describe('ViewMeetingCtrl', function () { var $rootScope, scope, $controller , $q ; beforeEach(angular.mock.module('MyApp')); beforeEach(inject(function ($rootScope, $controller ) { scope = $rootScope.$new(); cr
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 be equal to an object', function () {
var controller = new createController();
//some assertion
});
});
以下是我的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
}
})();
我的问题是,如何在上述cancelMeeting()上调用spyOn(或任何其他与jasmine spies相关的方法)方法,以便模拟方法调用、返回等。我做了以下操作
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 be equal to an object', function () {
spyOn(scope, 'cancelMeeting');//cancelMeeting is inside the scope so did like this
var controller = new createController();
});
});
但我得到以下输出
Firefox 37.0.0 (Windows 8.1) ViewMeetingCtrl the meeting type should be equal to an object FAILED
Error: cancelMeeting() method does not exist in C:/Users/Work/MyApp/Tests/node_mo
dules/jasmine-core/lib/jasmine-core/jasmine.js (line 1895)
我调用spyOn的方式是错误的还是缺少其他语法?。还是我遗漏了一些基本的东西?您的测试代码看起来不错。我想你只需要改变你作业的顺序。首先定义取消会议,然后分配会议
function cancelMeeting(meetingId, companyId) {
meetingService.sendCancelNotices(companyId, meetingId)
.success(function () {
$state.go('company.view');
});
}
$scope.cancelMeeting = cancelMeeting;
或者只是:
$scope.cancelMeeting = function(meetingId, companyId) {
meetingService.sendCancelNotices(companyId, meetingId)
.success(function () {
$state.go('company.view');
});
}
在创建控制器之前,
cancelMeeting
功能不会添加到作用域中。所以我认为您只需要反转测试代码中的行:
it('the meeting type should be equal to an object', function () {
var controller = new createController();
spyOn(scope, 'cancelMeeting');
});
非常感谢。你是对的,在conntroller创建之前调用spyOn是个问题:)谢谢你的回复。但问题是在创建控制器之前调用spyOn,请参见John Case的回复要点。但是为什么不直接在“beforeach”中创建控制器呢?然后可以立即调用spyOn方法。和示例(不管注释中的格式是否错误):变量范围、控制器;beforeach(注入(函数($rootScope,$controller){scope=$rootScope.$new();controller=$controller('yourController',{$scope:scope});});它('willtestsomething',inject(函数(){spyOn(scope,“getMethod”)。和.callFake(函数(){return“something”;});