Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine and angularjs, can't stub $httpBackend for service

I can't seem to return the expected results after stubbing $httpBackend, and I don't know what is wrong with the test. Can you please take a look at this failing minimal example?

Link with demo

http://jsfiddle.net/paos/kTNF5/

Jasmine Output

query() Expected [ ] to equal [ { hello : 'world' } ].

Code

Dependant resources
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine-html.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="http://code.angularjs.org/1.0.1/angular-mocks-1.0.1.js"></script>
Test javascript
var mod = angular.module('mod', ['ngResource']);                                                                                                                               

mod.factory('Brief', function($resource) {
   var Brief = $resource('http://some_test/:id');                                                                                                                              
   return Brief;                                                                                                                                                               
});                                                                                                                                                                            

beforeEach(function() {
    module('mod');                                                                                                                                                             

    inject(function($injector) {
        resource = $injector.get('$resource');                                                                                                                                 
        $httpBackend = $injector.get('$httpBackend');                                                                                                                          
        Brief = $injector.get('Brief');                                                                                                                                        
    });                                                                                                                                                                        

});                                                                                                                                                                            

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

describe("Brief", function() {
    it("query()", function() {
        var getRequest = 'http://some_test';                                                                                                                                   
        var fakeGetResponse = [{
            "hello": "world"}];                                                                                                                                                

        $httpBackend.when('GET', getRequest).respond(fakeGetResponse);                                                                                                         

        var briefs = Brief.query();                                                                                                                                            
        expect(briefs).toEqual(fakeGetResponse);                                                                                                                               
        $httpBackend.flush();                                                                                                                                                  
    });                                                                                                                                                                        
});                                                                                                                                                                            


// KICK OFF JASMINE
var jasmineEnv = jasmine.getEnv();                                                                                                                                             
var trivialReporter = new jasmine.TrivialReporter();                                                                                                                           

jasmineEnv.addReporter(trivialReporter);                                                                                                                                       

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

jasmineEnv.execute();​               

Thanks!

like image 832
user681814 Avatar asked Dec 02 '12 09:12

user681814


1 Answers

You've been almost there, but still there were 2 issues in your test:

  • expectation was verified before the $httpBackend was flushed. It is the $httpBackend.flush(); that simulates an incoming response
  • in your expectation you were comparing object instances (and not their values) but AngularJS $resource will create a new array as described in https://stackoverflow.com/a/11966512/1418796. You need to compare objects for their values not references. One way of doing so is: expect(angular.equals(briefs, fakeGetResponse)).toBeTruthy();

Finally here is a jsFiddle with a test passing: http://jsfiddle.net/VFqFU/

like image 105
pkozlowski.opensource Avatar answered Nov 09 '22 11:11

pkozlowski.opensource