使用Jasmine在Angular服务中测试$ http

时间:2016-05-19 22:39:30

标签: angularjs karma-jasmine

我很难在Angular服务中测试$ http。服务代码在我的模块中工作,但我似乎无法成功测试它。我是否需要更改服务结构的方式? $ httpBackend在下面列出的任何一种情况下都没有被击中。

我的服务:

<Setter Property="IsEnabled" Value="{Binding EnableOrder}" />

我的测试:

angular.module('myApp')

.service('backendApi', [
  "$http"
  function(
    $http
  ){
    this.getThis = function(getPath) {
      return $http({method: 'GET', url: getPath});
    };
  }
]);

1 个答案:

答案 0 :(得分:0)

这最终成为一个简单的解决方案。我需要刷新后端以应用更改。

backendApi.getThis('http://jsonplaceholder.typicode.com/posts/1').then(function(response.data) {
   expect(response.data).toEqual({ 'mocked': 'data' });
});

httpBackend.flush();