$ httpBackend.verifyNoOutstandingRequest()不会引发错误

时间:2013-08-09 09:55:03

标签: javascript angularjs testing

我的测试设置了一个http请求,但我没有执行$ httpBackend.flush()。这是测试:

describe("Unit: Testing Services", function() {
    beforeEach(angular.mock.module('EmsWeb.Services'));
    describe("Unit: DalService", function () {
        var $httpBackend, dalService, appModel;
        beforeEach(inject(function ($q, _$httpBackend_, AppModel, DalService) {
            dalService = DalService;
            appModel = AppModel;
            $httpBackend = _$httpBackend_;
            $httpBackend.when('POST', '../api/ModuleApi/GetModules').respond({ userId: 'userX', 'A-Token': 'xxx' });            
            appModel.context.company = { code: 'LPL' };
        }));

        describe("canceling request should fail", function () {             

            it('checks request should be canceled ', function () {
//              $httpBackend.expectPOST('../api/ModuleApi/GetModules');
                var returnedData;
                var callBackFn = function (data) {
                    returnedData = data;
                };
                dalService.getModules('senderIdentifier', appModel.context, callBackFn, callBackFn);
            });

        });

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

我希望$httpBackend.verifyNoOutstandingRequest();引发错误,因此无法调用$httpBackend.flush()

我还尝试添加$httpBackend.expectPOST('../api/ModuleApi/GetModules');,但它没有任何区别。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

$ httpBackend.verifyNoOutstandingRequest()不会引发有关“未刷新请求”的任何错误,除非发送$ http请求。 如果您共享DalService以及该服务中的getModules是什么,事情就会变得更加清晰。