使用Jasmine测试AngularJS的.then()的返回值

时间:2017-03-28 22:45:22

标签: angularjs jasmine karma-jasmine

这是我的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,内容以及来自哪里?

1 个答案:

答案 0 :(得分: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;
});