7

EDIT: I solved the problem by downgrading angular-mocks to 1.3.x (I'm using AngularJS 1.3.x)


I'm learning to write and run test on my Angular app with Karma and Jasmine.

The first tests passed, but when implementing $httpBackend, I got the following error:

TypeError: $browser.cookies is not a function
        at sendReq (/typotheque-web/public/app/bower_components/angular/angular.js:9661:24)
        at serverRequest (/typotheque-web/public/app/bower_components/angular/angular.js:9383:16)
        at processQueue (/typotheque-web/public/app/bower_components/angular/angular.js:13248:27)
        at /typotheque-web/public/app/bower_components/angular/angular.js:13264:27
        at Scope.$eval (/typotheque-web/public/app/bower_components/angular/angular.js:14466:28)
        at Scope.$digest (/typotheque-web/public/app/bower_components/angular/angular.js:14282:31)
        at Function.$httpBackend.flush (/typotheque-web/public/app/bower_components/angular-mocks/angular-mocks.js:1510:38)
        at Object.<anonymous> (/typotheque-web/public/test/libraryServiceSpec.js:74:17)

The incriminated piece of code...

// ...

var library, $httpBackend, BACKEND_URL;

beforeEach(inject(function(_Library_, _$httpBackend_) {
    library = _Library_;
    $httpBackend = _$httpBackend_;
    BACKEND_URL = 'http://srvpartage.local:3000';
}));

// ...

describe('updateFonts()', function() {
    it('should request the fonts to the server', function() {
        var fonts;
        $httpBackend.expectGET(BACKEND_URL+'/fonts').respond(200, [{}, {}]);

        library.updateFonts(function(data) {
            fonts = data;
        });

        $httpBackend.flush();

        expect(fonts.length).toBeGreaterThan(0);
    });
});

And the updateFonts() method:

this.updateFonts = function(callback) {
    return $http.get(BACKEND_URL+'/fonts')
    .success(function(data) {
        console.log('Library :: fonts updated', data);
        self.fonts = data;
        self.state.fonts.loaded = true;
        self.state.fonts.error = false;
        if(callback) callback(self.fonts);
    }).error(function() {
        self.state.fonts.error = true;
    });
};

When I remove this piece, the tests run fine. In any case, I referenced the angular-cookie library in my Karma configuration, but I still got the error.

I don't understand why I'm getting this error, as I don't make use of cookies through the whole app.

ryancey
  • 1,037
  • 2
  • 11
  • 27
  • 5
    Did you take a look at the solution from http://stackoverflow.com/questions/30487943/get-error-when-try-to-use-jasmine-and-angular? – ebo Jun 02 '15 at 11:32
  • 1
    I did not came across this question when I did my research. Thank you, it solved my problem. – ryancey Jun 02 '15 at 12:21

0 Answers0