加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:09:26 所属栏目:安全 来源:网络整理
导读:这是我的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.expect
这是我的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;
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读