Angular 1.2.10升级杀死了$ httpBackend测试

时间:2014-02-03 19:56:27

标签: ajax angularjs unit-testing qunit httpbackend

我刚从Angular.js 1.1.3升级到1.2.10,我的$httpBackend测试开始失败。我使用QUnit 1.12.0作为测试框架。

我的测试中有以下设置。

(function () {
var fittingDataServiceMock,
    injector,
    ctrl,
    $scope,
    $httpBackend;

module("Fitting Controller Test", {
    setup: function() {
        injector = angular.injector(['ngMock', 'ng','fittingApp']);
        $scope = injector.get('$rootScope').$new();
        $httpBackend = injector.get('$httpBackend');
        fittingDataServiceMock = injector.get('fittingDataService');

        //expects a post on controller creation
        $httpBackend.expectPOST('/app_dev.php/client_api/command').respond("hello");
        ctrl = injector.get('$controller')(DoubleVariableController, { $scope: $scope, fittingDataService: fittingDataServiceMock });
        $httpBackend.flush();
    },
    teardown: function() {

    }

    test ("DoubleVariableController Simple Test", function() {
    $httpBackend.expectPOST('/app_dev.php/client_api/command').respond(200, {data: "Hello"});

    // make an AJAX Posts

    $httpBackend.flush();
    }
});

在这种情况下,我得到两个失败的测试

第一次错误:

Setup failed on DoubleVariableController Simple Test: No pending request to flush!

    at Error (native)
    at Function.$httpBackend.flush (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular-mocks.js:1148:34)

第二次错误:

Died on test #2 ... No pending request to flush !

Error: No pending request to flush !
    at Error (native)
    at Function.$httpBackend.flush (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular-mocks.js:1148:34)

我不知道为什么所有的突然发布请求都没有通过。使用Angular 1.1.3,测试工作正常。有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

由于1.2.x已将ngRoute移动到单独的文件,您必须在测试配置文件中的angular.js之后包含它。