这是我的 AngularJS:
self.fetch = {
things: function() {
return $http.get("/things/")
.then(function(response) {
return response.data;
});
}
};
这是我的测试:
it('fetch.things() should GET to /things/, success', function() {
mockBackend.expectGET("/thingss/").respond({stuffs: 'stuffs'});
var response = BaseService.fetch.things();
mockBackend.flush();
expect(response).toEqual({stuffs: 'stuffs'})
});
这是我得到的错误:
Expected Object({ $$state: Object({ status: 1, value: Object({ stuffs: 'stuffs' }) }) }) to equal Object({ stuffs: 'stuffs' }).
at Object.<anonymous> (tests/test_base.js:28:26)
Chromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.102 seChromium 53.0.2785 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.119 secs / 0.102 secs)
那个额外的 status: 1,
东西是什么?它来自哪里?
最佳答案
在测试中尝试以下操作;
it('fetch.things() should GET to /things/, success', function() {
mockBackend.expectGET("/things/").respond({stuffs: 'stuffs'});
var p = BaseService.fetch.things();
mockBackend.flush();
p.then(function(response){
expect(response.data).toEqual({stuffs: 'stuffs'})
});
$rootScope.digest();
return p;
});
关于angularjs - 使用 Jasmine 测试 AngularJS 的 .then() 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43081246/