我是承诺的新手,似乎有实际的代码可以运行,但也尝试编写单元测试......
继承人我的控制人员:
appModule.controller('MyController', function($scope, myService, $timeout, mySecondService) {
$scope.accept = false;
$scope.loading = false;
$scope.text = 'Some text';
$scope.testCall = function() {
$scope.person = true;
$scope.text = '';
$scope.loading = true;
myService.getWeather()
.then(function(data) {
if (data.valid===true) {
$timeout(function(){
$scope.loading = false;
$scope.accept = true;
$timeout(function(){
$scope.customer.cart = false;
}, 1400);
}, 1500);
}
else {
$scope.person = false;
$scope.newMessage = mySecondService.getItem();
}
}, function(error) {
$scope.person = false;
$scope.newMessage = mySecondService.getItem();
});
}
});
ControllerSpec:
beforeEach(module('myApp'));
describe("MyController Tests", function() {
var scope;
var ctrl;
var customerInfo = {
"ID" : "212143513",
"firstName" : "Lewis",
"lastName" : "Noel"
};
beforeEach(inject(function($rootScope, $controller, myService) {
scope = $rootScope.$new();
rootScope = $rootScope;
mockMyService = myService;
ctrl = $controller('MyController', {$scope:scope, myService:mockMyService});
spyOn(scope, 'testCall').and.callThrough();
}));
it("On controller load, initialise variables", function() {
expect(scope.text).toEqual('Some text');
expect(scope.loading).toEqual(false);
expect(scope.accept).toEqual(false);
});
});
以上测试$ scope的初始化,但服务中没有任何内容......
这是我的服务:
appModule.factory('myService', function ($http, $q) {
return {
getWeather: function() {
return $http.get('/cart/customerCart/546546')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
}
else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
答案 0 :(得分:1)
如果你问的是如何一般地测试服务,这就是我的工作:
describe("myService", function() {
var myService;
beforeEach(inject(function($rootScope, _myService_) {
myService = _myService_;
}));
it("On controller load, initialise variables", function() {
var result;
myService.getWeather().then( function (data) {
result = data;
});
$rootScope.$apply();
expect(result).toEqual(whatever);
});
});
注意:
编辑: 你错误地使用了承诺,我会重构你的工厂(未经测试):
appModule.factory('myService', function ($http, $q) {
return {
getWeather: function() {
var defer = $q.defer();
$http.get('/cart/customerCart/546546')
.then(function(response) {
if (typeof response.data === 'object') {
defer.resolve(response.data);
}
else {
// invalid response
defer.reject(response.data);
}
}, function(response) {
// something went wrong
defer.reject(response.data);
});
return defer.promise;
}
};
});