0

I've been trying to fix it for hours. Unfortunately with negative effect so please help me.

In my application I want to test my $http requests. I'm doing it by using $httpBackend.

Here is my controller code:

angular.module('app').controller('testController',function($scope,$http){
 $http.get('/test/users').success(function(data){
        console.log('wtf');
    });
})

My unit test code:

describe('testController tests', function(){
    var $httpBackend, $scope, createController;
    beforeEach(module('app'));
    beforeEach(inject(function($injector, ngTableParams, notifier,identity, $sessionStorage, $location){

        $httpBackend = $injector.get('$httpBackend');

        $httpBackend.whenGET('/test/users').respond({test: 'test'});

        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();
        var $controller = $injector.get('$controller');


        createController = function(){
          return $controller('testControler', { '$scope': $scope, '$http': $httpBackend });
        };


    }));

    it('should fetch users ', function() {
        $httpBackend.expectGET('/test/users');
        var controller = createController();
        $httpBackend.flush();
    });

});

It's not working. I always have the following error:

TypeError: Object function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
var xhr = new MockXhr(),
    expectation = expectations[0],
    wasExpected = false;

function prettyPrint(data) {
  return (angula...<omitted>... } has no method 'get'
at new <anonymous> (http://localhost:9876/base/public/app/controllers/testController.js:256:11)
at invoke (http://localhost:9876/base/public/libraries/angular/angular.js:3805:17)
at Object.instantiate (http://localhost:9876/base/public/libraries/angular/angular.js:3816:23)
at $get (http://localhost:9876/base/public/libraries/angular/angular.js:6922:28)
at createController (http://localhost:9876/base/test/unit/testControllerSpec.js:70:18)
at null.<anonymous> (http://localhost:9876/base/test/unit/testControllerSpec.js:89:26)
at jasmine.Block.execute (http://localhost:9876/base/node_modules/karma-jasmine/lib/jasmine.js:1145:17)
at jasmine.Queue.next_ (http://localhost:9876/base/node_modules/karma-jasmine/lib/jasmine.js:2177:31)
at http://localhost:9876/base/node_modules/karma-jasmine/lib/jasmine.js:2167:18 

Any ideas what does it mean and how to fix it?

1
  • try to use .expectGET instead of .whenGET (in beforeEach)... and in use case, $httpBackend.expectGET('/test/users'); is not necessary Commented Apr 3, 2014 at 13:59

2 Answers 2

1

I've found error in my code. $httpBackend shouldn't be injected into controller.

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is you need to add the dependencies to your module creation:

angular.module('app', []).controller ...

instead of

angular.module('app').controller ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.