0

I put the code in a fiddle so it can be easily updated and 'worked with' if needed.

describe('PlayersListCtrl', function() {  // Jasmine Test Suite

    beforeEach(module('wc2014App'));

    var ctrl, scope, $httpBackend;

    beforeEach(inject(function($controller, $rootScope) {

        scope = $rootScope.$new();

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

    it('should have an empty player array', function() { 
        expect(scope.players.length).toBe(0);
    });

    describe('PlayersListCtrl', function() {
        var $httpBackend, $rootScope, createController;

        beforeEach(inject(function($injector) {
            $httpBackend = $injector.get('$httpBackend');
            $httpBackend.when('GET', '../app/stubs/players.json').respond(
                {userId: 'userX'}, 
                {'A-Token': 'xxx'});

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

            createController = function() {
                return $controller('PlayersListCtrl', {'$scope' : $rootScope });
            };
        }));

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

        it('should fetch authentication token', function() {
            $httpBackend.expectGET('../app/stubs/players.json');
            var controller = createController();
            $httpBackend.flush();
        });
    });
});

The rest, cause its quite verbose, is in the fiddle: http://jsfiddle.net/tLte2/

Basically the first test passes, not a hard one, but the second one depends on a JSON stub and gives errors like: PhantomJS 1.9.7 (Mac OS X) PlayersListCtrl PlayersListCtrl should fetch authentication token FAILED Error: No pending request to flush !

Cant seem to get a grip on how this $httpBackend stiff works. Is must be possible to just fire it and set the result in the scope of the controller?

--edit Basically got everything wired up perfectly and can do some simple tests that run just fine, however getting JSON stub data in there seems to be a pain. Workaround can be just defining the array described in the the JSON on the controller scope like: controller.players = ['one','two','three',..... etc ......] But that doesnt feel right. That $httpBackend stuff shouldn't be that hard to fix right?

scniro
  • 16,844
  • 8
  • 62
  • 106
San Jay Falcon
  • 993
  • 1
  • 9
  • 20
  • Don't try to circumvent the rules. Post the relevant code here. If it's too long, reduce it so that it only contains what is needed to reproduce the problem. – JB Nizet Jul 08 '14 at 13:02
  • 1
    The message says what the problem is: your test expects that the controller sends a GET request to '../app/stubs/players.json', and the httpBackend never receives one. Note that I indented your code to make it readable, even though that's your job. – JB Nizet Jul 08 '14 at 14:01
  • General comment: Injecting `$injector` with `inject` so you can get service instances with `$injector.get` is duplication of effort and a waste of keystrokes. You did it right in your first `beforeEach`. – moribvndvs Jul 08 '14 at 14:02
  • What does your controller do? The error message you're getting would suggest that it isn't making a GET request to ../app/stubs/players.json when it is instantiated – rob Jul 08 '14 at 14:38
  • got it working thx to input from JB Nizet – San Jay Falcon Jul 09 '14 at 12:07

0 Answers0