【问题标题】:Obtaining $httpBackend in QUnit tests在 QUnit 测试中获取 $httpBackend
【发布时间】:2013-04-30 12:57:46
【问题描述】:

我正在为 Angular 控制器编写 QUnit 测试。在module的setup函数中,我写了如下语句来获取$httpBackend的一个对象:

 var injector = angular.injector(['ng']);
 var httpBackend = injector.get('$httpBackend');

在测试中,GET 的模拟响应配置如下:

httpBackend.expectGET(url).respond([]);

测试规范在此语句中失败并出现错误:对象不支持属性或方法expectGET

我能够使用相同的注入器引用获取其他对象,例如控制器、作用域。

我错过了什么吗?

【问题讨论】:

    标签: javascript unit-testing angularjs qunit


    【解决方案1】:

    我向Scott Allen 询问了这个问题,他回复我JSFiddle 解决了这个问题。他表示,当我们在 QUnit 测试中使用 $httpBackend 或 angular-mocks.js 中定义的任何其他模拟时,以下装饰器是必不可少的:

    $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
    

    我在示例应用程序中使用了这种方法并将我的学习记录在博客中:http://sravi-kiran.blogspot.com/2013/06/UnitTestingAngularJsControllerUsingQUnitAndSinon.html

    【讨论】:

      【解决方案2】:

      您必须设置 Angular 才能使用来自 angular-mocks.js$httpBackend 其中包含expectGET 方法。

      但根据the docs,它是“仅适用于茉莉花。

      【讨论】:

        猜你喜欢
        • 2023-04-02
        • 1970-01-01
        • 2014-01-28
        • 2015-08-05
        • 1970-01-01
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多