我有一个称为返回承诺的服务,以及一个使用不同参数myHttp
调用两次的控制器。myHttp
为了测试控制器,我尝试myHttp
使用 Jasmine spyOn 进行模拟,如下所示:
beforeEach(inject(function($controller, _$rootScope_, _$q_, myHttp) {
$scope = _$rootScope_.$new();
$q = _$q_;
deferred = $q.defer();
deferred2 = $q.defer();
spyOn(myHttp, 'call').and.callFake(fake(1)).and.returnValue(deferred.promise);
spyOn(myHttp, 'call').and.callFake(fake(2)).and.returnValue(deferred2.promise);
wherefake
是检索要在myHttp
调用中使用的参数的函数。
问题是我不能声明两次相同的模拟函数。我从 Jasmine 收到以下错误:
错误:呼叫已被监视
如何解决这个测试?这是PLUNK
Javascript:
angular.module("mymodule", [])
.service('myHttp', function($http,$q){
this.call = function(obj) {
var defer = $q.defer();
$http({url:obj.url, data:obj.data})
.then(function (response) {
defer.resolve(response);
});
return defer.promise;
};
})
.controller('ctl', function($scope,myHttp) {
$scope.read = function (id){
var data = {};
data.id = id;
myHttp.call({url:'/getStudent', data:data})
.then(function(response) {
$scope.id = response.id;
$scope.name = response.nm;
$scope.classId = response.clsid;
var data2 = {};
data2.id = $scope.classId;
myHttp.call({url:'/getClass', data:data2})
.then(function(response) {
$scope.className = response.nm;
});
});
};
});
describe('Testing a Controller that uses a Promise', function () {
var $scope;
var $q;
var deferred;
var $timeout;
beforeEach(module('mymodule'));
beforeEach(inject(function($controller, _$rootScope_, _$q_, myHttp) {
$scope = _$rootScope_.$new();
$q = _$q_;
deferred = $q.defer();
deferred2 = $q.defer();
spyOn(myHttp, 'call').and.callFake(fake(1)).and.returnValue(deferred.promise);
spyOn(myHttp, 'call').and.callFake(fake(2)).and.returnValue(deferred2.promise);
$controller('ctl', {
$scope: $scope,
myHttp: myHttp
});
$scope.read(1)
}));
function fake (option) {
if (option==1)
return {url:'/getStudent', data: {id: 1}};
else
return {url:'/getClass', data: {id: 10}};
}
it('should resolve two promises', function () {
var student = {
id: 1,
nm: "John",
clsid: 10
};
var clazz = {
id: 10,
nm: "Math"
};
deferred.resolve(student);
$scope.$apply();
deferred2.resolve(clazz);
$scope.$apply();
expect($scope.id).toBe(student.id);
expect($scope.name).toBe(student.nm);
expect($scope.classId).toBe(student.clsid);
expect($scope.className).toBe(clazz.nm);
});
});