AngularJS、摩卡、业力。测试控制器,模拟服务承诺
AngularJS, Mocha, Karma. testing controller, mocking service promise
我正在使用 AngularJS、Karma、Mocha、Chai、Chai-as-promised、Sinon。
我正在尝试下面的控制器。我在模拟服务用户和测试控制器中的 .then() 部分时遇到问题。
控制器代码
.controller('SignupPhoneCtrl', function(User, $scope) {
$scope.sendPhoneNumber = function(countryCode, phoneNumber){
User.sendPhoneNumber(countryCode, phoneNumber)
.then(function(result){
if(result == "success"){
//I WANT TO TEST THIS SECTION
return "success";
}
//I WANT TO TEST THIS SECTION
return "fail";
});
}
})
控制器测试代码
describe('Controller: Signup-phone', function() {
var scope;
var UserMock;
var SignupPhoneCtrl;
beforeEach(function() {
module('starter.controllers');
});
beforeEach(inject(function($rootScope, $controller, $q){
scope = $rootScope.$new();
UserMock = {
sendPhoneNumber: function(countryCode, phoneNumber){
var deferred = $q.defer();
// console.log("called"); //This part is being called
deferred.resolve('foo');
return deferred.promise;
}
};
SignupPhoneCtrl = $controller("SignupPhoneCtrl", {$scope: scope, User: UserMock});
}))
it('should return asdf', function(){
scope.$digest();
//WHAT SHOULD I WRITE HERE?
})
});
我试过下面的方法,但断言部分没有被调用。
scope.sendPhoneNumber(12, 53452123).then(function(result){
result.should.equal("fail"); //THIS SECTION IS NOT CALLED
})
我试过下面的方法,但它给出了错误:'undefined' 不是一个函数(评估 'promise.then.bind(promise)'
scope.sendPhoneNumber(12, 53452123).should.eventually.equal("fail");
sendPhoneNumber(12, 53452123)
returns Object{$$state: Object{status: 0}}
我尝试添加 sinon.spy(UserMock, 'sendPhoneNumber')
,但没有任何区别。
问题是,$scope.sendPhoneNumber
没有 return 任何东西。尝试
return User.sendPhoneNumber(...
在你的控制器方法中。
此外,如果您只是想在模拟中立即解决 promise,我发现这很好用并且可以节省您设置延迟对象的时间
UserMock = {
sendPhoneNumber: function(countryCode, phoneNumber){
return $q.when('foo');
}
};
也就是说,您可能希望能够控制每个测试中的解析值,这样更有意义...
var scope;
var UserMock;
var SignupPhoneCtrl;
var deferred;
// snip
inject(function($rootScope, $controller, $q) {
deferred = $q.defer();
UserMock = {
sendPhoneNumber: function() {
return deferred.promise;
}
};
});
// snip
it('tests for success', inject(function($rootScope) {
deferred.resolve('success');
scope.sendPhoneNumber(...).then(...);
$rootScope.$apply();
}));
it('tests for failure', inject(function($rootScope) {
deferred.resolve('not success');
scope.sendPhoneNumber(...).then(...);
$rootScope.$apply();
}));
我正在使用 AngularJS、Karma、Mocha、Chai、Chai-as-promised、Sinon。
我正在尝试下面的控制器。我在模拟服务用户和测试控制器中的 .then() 部分时遇到问题。
控制器代码
.controller('SignupPhoneCtrl', function(User, $scope) {
$scope.sendPhoneNumber = function(countryCode, phoneNumber){
User.sendPhoneNumber(countryCode, phoneNumber)
.then(function(result){
if(result == "success"){
//I WANT TO TEST THIS SECTION
return "success";
}
//I WANT TO TEST THIS SECTION
return "fail";
});
}
})
控制器测试代码
describe('Controller: Signup-phone', function() {
var scope;
var UserMock;
var SignupPhoneCtrl;
beforeEach(function() {
module('starter.controllers');
});
beforeEach(inject(function($rootScope, $controller, $q){
scope = $rootScope.$new();
UserMock = {
sendPhoneNumber: function(countryCode, phoneNumber){
var deferred = $q.defer();
// console.log("called"); //This part is being called
deferred.resolve('foo');
return deferred.promise;
}
};
SignupPhoneCtrl = $controller("SignupPhoneCtrl", {$scope: scope, User: UserMock});
}))
it('should return asdf', function(){
scope.$digest();
//WHAT SHOULD I WRITE HERE?
})
});
我试过下面的方法,但断言部分没有被调用。
scope.sendPhoneNumber(12, 53452123).then(function(result){
result.should.equal("fail"); //THIS SECTION IS NOT CALLED
})
我试过下面的方法,但它给出了错误:'undefined' 不是一个函数(评估 'promise.then.bind(promise)'
scope.sendPhoneNumber(12, 53452123).should.eventually.equal("fail");
sendPhoneNumber(12, 53452123)
returns Object{$$state: Object{status: 0}}
我尝试添加 sinon.spy(UserMock, 'sendPhoneNumber')
,但没有任何区别。
问题是,$scope.sendPhoneNumber
没有 return 任何东西。尝试
return User.sendPhoneNumber(...
在你的控制器方法中。
此外,如果您只是想在模拟中立即解决 promise,我发现这很好用并且可以节省您设置延迟对象的时间
UserMock = {
sendPhoneNumber: function(countryCode, phoneNumber){
return $q.when('foo');
}
};
也就是说,您可能希望能够控制每个测试中的解析值,这样更有意义...
var scope;
var UserMock;
var SignupPhoneCtrl;
var deferred;
// snip
inject(function($rootScope, $controller, $q) {
deferred = $q.defer();
UserMock = {
sendPhoneNumber: function() {
return deferred.promise;
}
};
});
// snip
it('tests for success', inject(function($rootScope) {
deferred.resolve('success');
scope.sendPhoneNumber(...).then(...);
$rootScope.$apply();
}));
it('tests for failure', inject(function($rootScope) {
deferred.resolve('not success');
scope.sendPhoneNumber(...).then(...);
$rootScope.$apply();
}));