简体   繁体   English

在QUnit测试中获取$ httpBackend

[英]Obtaining $httpBackend in QUnit tests

I am writing QUnit tests for an Angular controller. 我正在为Angular控制器编写QUnit测试。 In the setup function of module, I have written the following statements to get an object of $httpBackend : 在模块的setup函数中,我编写了以下语句来获取$ httpBackend的对象:

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

In a test, a mock response for GET is configured as follows: 在测试中,GET的模拟响应配置如下:

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

The test spec fails at this statement with error: Object doesn't support property or method expectGET 测试规范在此语句中失败,并显示错误:对象不支持属性或方法expectGET

I am able to get other objects like controller, scope injected using the same injector reference. 我能够使用相同的喷射器参考来获得其他对象,如控制器,示波器。

Did I miss anything here? 我在这里错过了吗?

I asked Scott Allen about this issue and he replied me with a JSFiddle that solves the issue. 我问斯科特-阿伦这个问题,他回答说我用的jsfiddle ,解决了这个问题。 He stated that the following decorator is essential when we use $httpBackend or any other mock defined in angular-mocks.js in QUnit tests: 他说当我们在QUnit测试中使用$ httpBackend或angular-mocks.js中定义的任何其他模拟时,以下装饰器是必不可少的:

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

I used this approach in a sample application and blogged my learning: http://sravi-kiran.blogspot.com/2013/06/UnitTestingAngularJsControllerUsingQUnitAndSinon.html 我在示例应用程序中使用了这种方法并在博客中写了我的学习内容: http//sravi-kiran.blogspot.com/2013/06/UnitTestingAngularJsControllerUsingQUnitAndSinon.html

You must setup angular to use the $httpBackend from angular-mocks.js Which contains the expectGET method. 您必须设置angular以使用angular-mocks.js中$ httpBackend ,其中包含expectGET方法。

But according to the docs it's " Only available with jasmine. " 但根据文件,它是“ 只有茉莉花可用 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM