vendredi 26 juin 2015

Mock $http Angular Ionic

I am using Ionic and I'd like to do some unit test on my httpClient

describe('send', function() {
    var $httpBackend, userManager, apiClient;

    beforeEach(inject(function($injector) {
        $httpBackend = $injector.get('$httpBackend');
        apiClient = new APIClient($httpBackend, userManager);
    }));

    it ('Should check if send() exists', function() {
        expect(apiClient.send).toBeDefined();
    });

    it ('Should send GET request', function(done) {
        var url = '/';

        $httpBackend.expect('GET', url, {}, {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}).respond({});
        apiClient.send({
            url: url,
            success: function(data, status) {
                console.log(data);
                console.log(status);
                done();
            },
            error: function(data, status) {
                console.log(data);
                console.log(status);
                done();
            }
        });
    });
});

But I have all time this error

Error: Unexpected request: [object Object] undefined
Expected GET /

I add a console.log just before the throw in angular-mock and I have

LOG: Object{method: 'GET', url: '/', headers: Object{Content-Type: 'application/x-www-form-urlencoded; charset=UTF-8'}, data: Object{}}

I also try with only $httpBackend.expect('GET', url) and I have the dame error

I tried with $httpBackend.when('GET', url) and I have

Error: Unexpected request: [object Object] undefined
No more request expected

Aucun commentaire:

Enregistrer un commentaire