我有以下茉莉花规格。
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(或任何其他茉莉花间谍相关方法)方法,以便我可以模拟方法调用,返回等。我做了以下
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的方式是错误的还是我错过了任何其他语法?或者我在这里遗漏了一些基本的东西?
答案 0 :(得分:2)
在创建控制器之前,cancelMeeting
函数未添加到作用域。所以我认为你只需要反转测试代码中的行:
it('the meeting type should be equal to an object', function () {
var controller = new createController();
spyOn(scope, 'cancelMeeting');
});
答案 1 :(得分:1)
您的测试代码看起来不错。我想你只需要改变作业的顺序。首先定义cancelMeeting,然后分配它。
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');
});
}