2

我刚刚从Angular.js 1.1.3to升级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 运行良好。有任何想法吗?

4

1 回答 1

-1

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

于 2014-03-10T12:56:38.277 回答