Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$httpBackend.whenGET() passThrough() not defined

I would like to pass some of my http request through and not mock them in my unit test, but when I try to call passThrough() method, an error of missing method is thrown:

"TypeError: Object # has no method 'passThrough'".

Does anybody know how I can fix it please?

There is my code:

'use strict';

describe('Controller: MainCtrl', function () {

    // load the controller's module
    beforeEach(module('w00App'));

    var scope, MainCtrl, $httpBackend;

    // Initialize the controller and a mock scope
    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
        $httpBackend = _$httpBackend_;
        $httpBackend.expectGET('http://api.some.com/testdata.json').passThrough();


        scope = $rootScope.$new();
        MainCtrl = $controller('MainCtrl', {
            $scope: scope
        });
    }));
});
like image 515
Adam Bernau Avatar asked Aug 11 '13 14:08

Adam Bernau


1 Answers

If you want to mock your backend during development, just install angular-mocks in your main html file, add it up as a dependency in your application (angular.module('myApp', ['ngMockE2E'])) and then mock the requests you need to.

For example;

angular.module('myApp')
  .controller('MainCtrl', function ($scope, $httpBackend, $http) {
    $httpBackend.whenGET('test').respond(200, {message: "Hello world"});
    $http.get('test').then(function(response){
      $scope.message = response.message //Hello world
    })
  });

Be wary though, that adding the ngMockE2E will require you to set up your routes in case you do so through AngularJS routing.

Example;

angular.module('myApp', ['ngMockE2E'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  .run(function($httpBackend){
    $httpBackend.whenGET('views/main.html').passThrough();
  })
like image 177
BKM Avatar answered Sep 24 '22 21:09

BKM