mercredi 22 avril 2015

Jasmine Unit Test for http

I am trying to write unit tests cases around an Angular service. What I am basically trying to test is that the http request is made.

Service

app.service("myService", [
    "$http", function ($http) {
        this.result = "initial value";
        this.get = function (param1, param2) {
            return $http({
                method: "GET",
                url: "api/someService/Get/" + param1 + "/" + param2
            })
                .success(function (data) {
                    this.parent.result = data;
                    console.log(data);
                        console.log(this.parent.result);
                    return data;
                })
                .error(function () {
                    return "Error occurred";
                })
            ;
        };
    }
]);

Below is what I have tried.

describe("Surcharge Increase Formula", function () {
    var controller;
    var scope;
    var httpBackend;
    var myService;

    beforeEach(inject(function($controller, $rootScope, $httpBackend, _myService_) {
        scope = $rootScope.$new();
        httpBackend = $httpBackend;
        myService = _myService_;
    });

    it('waiver service makes API call', function () {
        var response = "This is the response";
        httpBackend.when('GET', "api/SurchargeWaiver/Get/0/0").respond(response);

        waiverService.get(0, 0);
        //httpBackend.flush();

        expect(waiverService.result).toEqual(response);


    });

What I would like to do is make a call similar to expect(...).toHaveBeenCalled() to see if the api call has been made, or any other way to just test the http call.

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire