我正在尝试为角度控制器编写单元测试,其中变量从服务调用中分配值。
我可以找到直到json调用的方式但是当我尝试调用api时,我收到错误。我不知道如何将我的服务调用映射到测试中。
控制器
var vm = this;
vm.creationDate = 8; // test passed
vm.mainServiceNumber = MainService.getNumber();// test passed
vm.addNumberFunction = function(a, b){// test passed
vm.sumOfTwoNumbers = MainService.addNumbers(a, b);
}
MainService.getNumberFromJson().then(function(response){// test failed
vm.numberFromJson = response;
})
var id =10;
vm.getNumberFunction = function(id){// test failed
MainService.getNumberJsonFunction(id).then(function(response){
vm.numberFromJsonFunction = response;
})
}
服务
var service = this;
service.getNumber = function(){
return 8;
}
service.addNumbers = function(a, b){
return a+b;
}
service.getNumberFromJson = function(){
var num = {};
num = $q.defer();
num.resolve(7);
return num.promise;
}
service.getNumberJsonFunction = function(id){
var deferred = {};
deferred = $q.defer();
deferred.resolve(10);
return deferred.promise;
}
测试用例
var vm;
var MainService;
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_, _MainService_) {
vm = _$controller_('MainController');
MainService = _MainService_;
}));
/*it('should have a timestamp creation date', function() {
expect(vm.creationDate).toEqual(jasmine.any(Number));
});*/
it('should have a value', function() {
expect(vm.creationDate).toEqual(8);
});
it('should get a value from service', function() {
expect(vm.mainServiceNumber).toEqual(8);
});
it('should get a value from service after addition', function() {
vm.addNumberFunction(3, 3);
expect(vm.sumOfTwoNumbers).toEqual(6);
});
it('should get a value from service after a service call without passing params', function() { //Test Failed
expect(vm.numberFromJson).toEqual(7); //Expected undefined to equal 7.
});
it('should get a value from service after a service call passing params', function() {// Test Failed
vm.getNumberFunction(10)
expect(vm.numberFromJsonFunction).toEqual(10);//Expected undefined to equal 10.
});
我删除了所有测试代码,例如spyon,$ http以保持代码简单&干净。
我正在使用https://github.com/Swiip/generator-gulp-angular进行角度项目的基本设置
答案 0 :(得分:0)
以下是成功测试案例的一天。
describe('controllers', function(){
var vm;
var MainService;
var $timeout;
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_,_$timeout_, _MainService_) {
vm = _$controller_('MainController');
spyOn(_MainService_, 'getNumberFromJson').and.returnValue(7);
spyOn(_MainService_, 'getNumberJsonFunction').and.callThrough();
$timeout = _$timeout_;
}));
/*it('should have a timestamp creation date', function() {
expect(vm.creationDate).toEqual(jasmine.any(Number));
});*/
it('should have a value', function() {
expect(vm.creationDate).toEqual(8);
});
it('should get a value from service', function() {
expect(vm.mainServiceNumber).toEqual(8);
});
it('should get a value from service after addition', function() {
vm.addNumberFunction(3, 3);
expect(vm.sumOfTwoNumbers).toEqual(6);
});
it('should get a value from service after a service call without passing params', function() {
$timeout.flush();
expect(vm.numberFromJson).toEqual(7);
});
it('should get a value from service after a service call passing params', function() {
vm.getNumberFunction(7)
$timeout.flush();
expect(vm.numberFromJsonFunction).toEqual(7);
});
});