AngularJS Issues mocking httpGET request

后端 未结 1 1931
时光说笑
时光说笑 2021-01-13 18:00

so I\'m new to angularjs and its mocking library. I am trying to test that a specific GET request is made, but I always get this error for the 2nd assertion and can\'t figur

相关标签:
1条回答
  • I finally got my unit tests working! Mostly because I restructured my application to make more sense and be more modular.

    I'll try to give information to help the next person that runs into this:

    first of was I switched to using the $resource instead of $http.

    instead of injecting $injector, I injected $httpBackend like so:

    beforeEach(inject(function(_$httpBackend_, $rootScope, $route,  $controller){
    
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET('/path/to/api').respond([{id:1}]);
    

    instead of referencing 'Ctrl' as a string, I passed in the actual class

    Ctrl = $controller('Ctrl', { $scope: scope });

    became

    var ProductsCtrl = ['$scope', function($scope){ ... }];
    
    Ctrl = $controller(ProductsCtrl, {
      $scope: scope
    });`
    

    Make sure you are referencing the angular-resources.js file if you are using $resources

    I'm really loving Angularjs; I think it just takes some time to wrap your head around how to test. Best of luck out there!

    0 讨论(0)
提交回复
热议问题