AngularJS : Testing factory that returns $http promises

徘徊边缘 提交于 2019-12-02 04:14:12

A few things need to be changed

  • Use whenGET instead of expectGET in order to fake a response
  • In the test then callback, set the response to a variable available outside the callback so you can test it in an expect call
  • Make sure the expect call is outside any callbacks, so it always runs and shows any failures.

Putting it all together:

it('gets a user and updates customProperty', function () {
  $httpBackend.whenGET('/api/users/123').respond({ id: 123 });
  User.get(123).then(function(response) {
    user = response;
  })
  $httpBackend.flush();
  expect(user.customProperty).toBe(true); 
});

As can be seen at http://plnkr.co/edit/9zON7ALKnrKpcOVrBupQ?p=preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!