获得$ httpBackend在QUnit测试测试、httpBackend、QUnit

2023-09-13 04:53:38 作者:念念清风起

我写的 QUnit 为角控制器测试。在模块的设置功能,我写了下面的语句来获取对象的 $ httpBackend

I am writing QUnit tests for an Angular controller. In the setup function of module, I have written the following statements to get an object of $httpBackend:

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

在一个试验中,获得了模拟响应的配置如下:

In a test, a mock response for GET is configured as follows:

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

测试规范在失败与错误语句:对象不支持属性或方法 expectGET

我能够得到其他对象,如控制器,使用相同的注射器参考范围注入。

I am able to get other objects like controller, scope injected using the same injector reference.

我错过了什么吗?

推荐答案

我问斯科特 - 阿伦关于这个问题,他回答我一个的jsfiddle ,解决了问题。他说,下面的装饰是必不可少的,当我们使用$ httpBackend或QUnit的测试中角mocks.js定义的任何其他模拟:

I asked Scott Allen about this issue and he replied me with a JSFiddle that solves the issue. He stated that the following decorator is essential when we use $httpBackend or any other mock defined in angular-mocks.js in QUnit tests:

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

我以前在一个示例应用程序这一做法,并在博客我的学习:的http://sravi-kiran.blogspot.com/2013/06/UnitTestingAngularJsControllerUsingQUnitAndSinon.html