Angular unit tests - httpBackend

Demonstrating usage of module and inject from angular mocks

HTML

<script src="http://jasmine.github.io/1.3/lib/jasmine.js"></script>
<script src="http://jasmine.github.io/1.3/lib/jasmine-html.js"></script>
<link rel="stylesheet" href="http://jasmine.github.io/1.3/lib/jasmine.css">
<script src="http://code.angularjs.org/1.0.5/angular.js"></script>
<script src="http://code.angularjs.org/1.0.5/angular-mocks.js"></script>

JavaScript

// --- SPECS -------------------------
var url = '/path/to/resource';
var result = '';

describe('$httpBackend', function () {


    it("expects GET different results in subsequent requests", inject(function ($http, $httpBackend) {
            
        successCallback = function(data){
            result = data;            
        }
        // Create expectation
        $httpBackend.expectGET(url).respond(200, 'mock data');
        
        // Call http service
        $http.get(url).success(successCallback);
        
        // flush response
        $httpBackend.flush();
        console.log( result );
        
        // Verify expectations
        expect( result ).toContain('mock data');
        
        // reset the expectations
        $httpBackend.resetExpectations();
        
        // set the fake data AGAIN
        $httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon');
        
        // get the service AGAIN
        $http.get(url).success(successCallback);
        $httpBackend.flush();
        expect( result ).toContain('doof');
        console.log( result );
        
    }));

});

// --- Runner -------------------------
(function () {
    var jasmineEnv = jasmine.getEnv();
    jasmineEnv.updateInterval = 1000;

    var htmlReporter = new jasmine.HtmlReporter();

    jasmineEnv.addReporter(htmlReporter);

    jasmineEnv.specFilter = function (spec) {
        return htmlReporter.specFilter(spec);
    };

    var currentWindowOnload = window.onload;

    window.onload = function () {
        if (currentWindowOnload) {
            currentWindowOnload();
        }
        execJasmine();
    };

    function execJasmine() {
        jasmineEnv.execute();
    }

})();