2
我想測試一個url的響應狀態,保存在一個對象中,但是karma顯示了以下錯誤:Expected Promise({& & state:Object({status:0})}} )爲(whatelse)Jasmine http return promise
彌服務:
angular.module('ServiceModule', []).
service('anonymous_Login_Services', ['$http','$q', function($http,$q){
var data={dato:null,estado:null};
this.loadData=function(){
var defered=$q.defer();
$http({
method: 'POST',
url: 'http://example.com/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: "grant_type=client_credentials"
})
.success(function(inf,state){
data.dato=inf;
data.estado=state;
defered.resolve(data);
})
.error(function(inf,state){
data.dato=inf;
data.estado=state;
defered.reject(data);
});
return defered.promise;
}
}]);
而且茉莉測試:
describe('Test servicios', function() {
var anomlogin, $httpBackend, $q;
beforeEach(module('ServiceModule', function($provide) {
}));
beforeEach(inject(function (_$httpBackend_,_$q_, _anonymous_Login_Services_) {
$httpBackend = _$httpBackend_;
anomlogin = _anonymous_Login_Services_;
$q = _$q_;
}));
it('Token', function() {
var status2=0;
var estado= anomlogin.loadData();
console.log(estado);
expect(anomlogin.loadData()).toBe(status2);
});
});
我不想用一個模擬的HTTP POST與httpbac肯德,我想比較對象,其中之一是通過服務中的帖子獲得的。
非常感謝。
http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-它 –