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

前端之家收集整理的这篇文章主要介绍了使用Jasmine测试AngularJS的.then()的返回值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的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)

什么是额外的状态:1,东西和它来自哪里?

解决方法

在测试中尝试以下方法;

it('fetch.things() should GET to /things/,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相关文章