$httpBackend 被忽略,Jasmine 调用实际端点

$httpBackend being ignored, Jasmine calling actual endpoint

我正在编写一个测试来简单地检查端点是否被服务调用。我正在使用 $httpBackend 来模拟这个响应,但是测试仍然在击中真正的后端端点,而不是使用 $httpBackend 模拟。

服务

function getOptions() {
   return $http.get('/apiv1/something').then(function (data) {
      return [{
         name: "eGo C"
         image: "http://www.google.co.uk/logo.png"
      }];
   };
}

测试

describe('product service', function () {

   var $httpBackend, service, $rootScope;

   var failTest = function (error) {
       expect(error).toBeUndefined();
   };

   beforeEach(module('appName'));

   beforeEach(inject(function (_$httpBackend_, productService, _$rootScope_) {
       $httpBackend = _$httpBackend_;
       $rootScope = _$rootScope_;
       service = productService;
   }));

   afterEach(function() {
       $httpBackend.verifyNoOutstandingExpectation();
       $httpBackend.verifyNoOutstandingRequest();
   });

   describe('getOptions', function () {
    it('should make a call to the backend', function () {

        var handler,
            myThings,
            errorStatus,
            mockResponce;

        mockResponce = [
            {
                name: 'evod kit',
                img: 'img.jpg'
            }
        ];

        myThings = [];
        errorStatus = '';

        handler = {
            success: function(data) {
                console.log("success " + data);
                myThings = data;
            },
            error: function(data) {
                console.log("error " + data);
                errorStatus = data;
            }
        };

        spyOn(handler, 'success').and.callThrough();
        spyOn(handler, 'error').and.callThrough()

        $httpBackend.when('GET', '/apiv1/something').respond(mockResponce);

        service.getOptions().then(handler.success, handler.error);
        $httpBackend.flush();

        console.log("MY THINGS ", myThings);
        // PROBLEM!!! == FROM THE SERVICE -> [{name: "eGo C"
        //        image: "http://www.google.co.uk/logo.png"
        //     }]
    });
});

试试这个:

 let $rootScope


     beforeEach(angular.mock.inject(function( _$httpBackend_, _$rootScope_){
        $rootScope = _$rootScope_
        $rootScope.httpbackend = _$httpBackend_
     }

本质上..我是个白痴。在服务中,我返回了一个硬编码数组,因为后端尚未构建。 $httpBackend 正在做它的工作并返回新数据,但当然,它没有被服务返回。叹。我需要一杯啤酒。