茉莉花单元测试为http

时间:2015-04-22 21:32:49

标签: angularjs unit-testing jasmine

我正在尝试围绕Angular服务编写单元测试用例。我基本上试图测试的是http请求。

服务

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";
                })
            ;
        };
    }
]);

以下是我的尝试。

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);


    });

我想做的是拨打一个类似于expect(...)。toHaveBeenCalled()的电话来查看是否已经进行了api调用,或者是否只是测试了http调用。

有什么建议吗?

0 个答案:

没有答案