AngularJS - Unit test for http get to JSON file

Oam Psy

I am trying to write a unit test to test a simple factory that performs a http.get to retrieve a JSON file.

The factory is called within my controller.

Here's a plunker showing my http.get: http://plnkr.co/edit/xg9T5H1Kreo4lwxzRQem?p=preview

Ctrl:

app.controller('MainCtrl', function($scope, $http, factoryGetJSONFile) { 

  factoryGetJSONFile.getMyData(function(data) {
    $scope.Addresses = data.Addresses.AddressList;
    $scope.People = data.Names.People;
  });

});

Factory:

app.factory('factoryGetJSONFile', function($http) {
  return {
    getMyData: function(done) {
      $http.get('data.json')
      .success(function(data) {
        done(data);
      })
      .error(function(error) {
        alert('An error occured whilst trying to retrieve your data');
      });
    }
  }
});

Test:

// ---SPECS-------------------------

describe('with httpBackend', function () {
    var app;
    beforeEach(function () {
        app = angular.mock.module('plunker')
    });

    describe('MyCtrl', function () {
        var scope, ctrl, theService, httpMock;

        beforeEach(inject(function ($controller, $rootScope, factoryGetJSONFile, $httpBackend) {
            scope = $rootScope.$new(),
            ctrl = $controller('MyCtrl', {
                $scope: scope,
                factoryGetJSONFile: theService,
                $httpBackend: httpMock
            });
        }));

        it("should make a GET call to data.json", function () {
                console.log("********** SERVICE ***********");
                  httpMock.expectGET("data.json").respond("Response found!");
                //expect(factoryGetJSONFile.getMyData()).toBeDefined();
                httpMock.flush();
            });

    })
});

Error:

TypeError: 'undefined' is not an object (evaluating 'httpMock.expectGET')
merlin

You should assign $httpBackend to httpMock in beforeEach like this:

   beforeEach(inject(function ($controller, $rootScope, factoryGetJSONFile, $httpBackend) {
        httpMock = $httpBackend;
        scope = $rootScope.$new();
        ctrl = $controller('MyCtrl', {
            $scope: scope,
            factoryGetJSONFile: factoryGetJSONFile,
            $httpBackend: httpMock
        });
    }));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get json file for karma unit test

From Java

AngularJS: factory $http.get JSON file

From Dev

HTTP Get in AngularJs Could not located the Json file

From Dev

AngularJS passing a parameter in an $http.get() to get a subset of a json file

From Dev

AngularJS passing a parameter in an $http.get() to get a subset of a json file

From Dev

How to unit test functions with $http.jsonp in AngularJS?

From Dev

How to unit test functions with $http.jsonp in AngularJS?

From Dev

AngularJS + Jasmine mock unit test $http factory of POST method

From Dev

AngularJS unit test with $http request never fires .then() callbacks

From Dev

Unit test of controller angularjs

From Dev

AngularJS - Unit Test with $timeout

From Dev

Unit test angularjs controller

From Dev

AngularJs unit test 'this'

From Dev

Angularjs Unit Test for Service

From Dev

Laravel unit test with upload file can not get file

From Dev

Unexpected HTTP GET calls from within a custom Service unit test

From Dev

mockito unit test cases for Response entity performing HTTP GET request

From Dev

Displaying json file to the view with $http in angularjs

From Dev

Displaying json file to the view with $http in angularjs

From Dev

Unable to return results from JSON flat file with HTTP get call in AngularJS

From Dev

AngularJS $http GET request to local JSON file returns SyntaxError: Unexpected token ]

From Dev

HTTP Status 0 from AngularJS Get for JSON

From Dev

AngularJS http.get verify valid json

From Dev

AngularJS HTTP Get JSON won't display

From Dev

Angularjs $http.get(). JSON fron server

From Dev

How to load a local json file in Xcode for unit test purposes?

From Dev

Unit Test Directive Controllers in AngularJS

From Dev

$httpBackend in AngularJs Jasmine unit test

From Dev

How to unit test angularjs form?

Related Related

  1. 1

    Get json file for karma unit test

  2. 2

    AngularJS: factory $http.get JSON file

  3. 3

    HTTP Get in AngularJs Could not located the Json file

  4. 4

    AngularJS passing a parameter in an $http.get() to get a subset of a json file

  5. 5

    AngularJS passing a parameter in an $http.get() to get a subset of a json file

  6. 6

    How to unit test functions with $http.jsonp in AngularJS?

  7. 7

    How to unit test functions with $http.jsonp in AngularJS?

  8. 8

    AngularJS + Jasmine mock unit test $http factory of POST method

  9. 9

    AngularJS unit test with $http request never fires .then() callbacks

  10. 10

    Unit test of controller angularjs

  11. 11

    AngularJS - Unit Test with $timeout

  12. 12

    Unit test angularjs controller

  13. 13

    AngularJs unit test 'this'

  14. 14

    Angularjs Unit Test for Service

  15. 15

    Laravel unit test with upload file can not get file

  16. 16

    Unexpected HTTP GET calls from within a custom Service unit test

  17. 17

    mockito unit test cases for Response entity performing HTTP GET request

  18. 18

    Displaying json file to the view with $http in angularjs

  19. 19

    Displaying json file to the view with $http in angularjs

  20. 20

    Unable to return results from JSON flat file with HTTP get call in AngularJS

  21. 21

    AngularJS $http GET request to local JSON file returns SyntaxError: Unexpected token ]

  22. 22

    HTTP Status 0 from AngularJS Get for JSON

  23. 23

    AngularJS http.get verify valid json

  24. 24

    AngularJS HTTP Get JSON won't display

  25. 25

    Angularjs $http.get(). JSON fron server

  26. 26

    How to load a local json file in Xcode for unit test purposes?

  27. 27

    Unit Test Directive Controllers in AngularJS

  28. 28

    $httpBackend in AngularJs Jasmine unit test

  29. 29

    How to unit test angularjs form?

HotTag

Archive