我想在我的角度框架來測試控制器的角度控制器:
.controller('UserCtrl', ['$scope', '$location', 'User', 'Config',
function ($scope, $location, User, Config) {
...
}])
該控制器取決於一對夫婦需要$ HTTP對象進行服務服務器調用:
.factory('User', ['$http', function ($http) {
var data = {};
return {
query: function(oper, putdata, callback){
if(oper == 'get'){
$http.get(getUrl("user",null)).success(function(data2) {
console.log(data2);
callback(data2);
data.userinfo = data2;
});
},
userinfo: data
};
}])
但是當我嘗試自舉控制器我不能讓httpBackend工作:
describe('UserCtrl', function(){
var ctrlScope, ctrl, $httpBackend, controllerService;
beforeEach(
inject(function($httpBackend, $http, $rootScope, $controller, User, Config) {
_User = User;
_Config = Config;
spyOn(User, 'getUserInfo').andCallThrough();
//spyOn(User, 'query').andCallThrough();
ctrlScope = $rootScope.$new();
controllerService = $controller;
httpMock = $httpBackend;
})
);
it('should create setup userinfo object ', function() {
httpMock.expectGET("/user/default/details").
respond({somejson});
ctrl = controllerService('UserCtrl', {$scope: ctrlScope, $location: location, User: _User, Config: _Config});
expect(_User.getUserInfo).toHaveBeenCalled();
httpMock.flush();
expect(ctrlScope.userinfo.length).toBe(1);
});
});
所有我曾經得到的是:
Error: No pending request to flush !
所以是有可能使用httpBackend與您從您正在測試一個名爲控制器的服務?
getUserInfo定義在哪裏?看起來你的方法仍然被稱爲'查詢'? – Rafi