延迟与$回应httpBackend [英] Delaying a response with $httpBackend

查看:200
本文介绍了延迟与$回应httpBackend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我已经加载所显示的动画,直到我收到来自API的响应。

In my view I have loading animation that is displayed until I receive a response from the API.

//Displayed before we've received API response
<p ng-if="vm.vehicles == null">Loading ...</p>

//Displayed once we received response for the API
<table ng-if="vm.vehicles">
    <tr ng-repeat="vehicle.vm.vehicles">...</tr>

要尽我的测试中,我使用 $ httpBackend 角模块。事情是这样的:

To do my testing, I use the $httpBackend Angular module. SOmething like this:

$httpBackend.whenGET('api/vehicles').respond({vehicles: [...]});

问题

我想写一个测试,以检查显示加载动画。

I want to write a test to check the the loading animation is displayed.

我试过:

expect(ptor.isElementPresent(By.cssContainingText('p', 'Loading'))).to.eventually.be.true;`

但我简化版,传球。所以,我想我需要有条件地拖延我从$ httpBackend得到响应。

but i does't pass. So i think I need to conditionally delay the response I get from $httpBackend.

我发现这个博客帖子<一个href=\"http://endlessindirection.word$p$pss.com/2013/05/18/angularjs-delay-response-from-httpbackend/\" rel=\"nofollow\">http://endlessindirection.word$p$pss.com/2013/05/18/angularjs-delay-response-from-httpbackend/

然而,插入了我的所有测试,配置方法失败(我想是因为模板不及时加载),它拖延所有响应,所以它不是真的是我需要的。

However, inserting that config method made all my tests fails (I think because the templates aren't loaded in time), it delays ALL responses so it's not really what I need.

所以,我怎么能推迟一个调用的响应?理想情况下,我想推迟它仅适用于测试。

So how can I delay the response for that one call? Ideally, I would like to delay it only for that test.

推荐答案

我能够得到这个通过超时,以解决对$ httpBackend的响应()方法可在函数中承诺的工作。

I was able to get this to work by using a timeout to resolve a promise inside the function available on the respond() method of $httpBackend.

我是测试对矿井的角度服务,填充与API调用的结果列表的方法。此外,我对那个服务指示当列表加载,当它完成的bool。所以在我的特别单元测试,我想简单地确保了加载布尔设置为true,然后回false完成后(并认为加载完成布尔是在该列表的成功填充设置为true。

I was testing a method on an angular service of mine that populates a list with the results from an API call. Additionally, I have bools on that service that indicate when the list is loading and when it is finished. So in my particular unit test I wanted simply to make sure the "loading" bool was set to true and then back to false when finished (and that the "finished loading" bool was set to true upon successful filling of that list.

要做到这一点,我需要耽误我的嘲笑httpBackend GET回应,让我检查加载布尔设置为true和加载完成布尔是不确定的,但随后的暂停过后双方的bool需要有新的价值观。

To do this, I needed to delay my mocked httpBackend GET response, allowing me to check that the "loading" bool was set to true and the "finished loading" bool was undefined, but then after the timeout elapsed both bools needed to have new values.

更新:道歉不更新此越快。我把一个$超时()之前回应里面,但是因为我没有执行$ timeout.flush()的$超时功能里面的期望发言不执行。我不延误模拟这种方式了,但我忘了我有这个张贴在这里。感谢@马克 - 阿梅里奥是带给我的注意。

UPDATE: Apologies for not updating this sooner. I was putting a $timeout inside the respond() before, however because I wasn't performing a $timeout.flush() the expect statements inside the $timeout function were not executing. I don't simulate delays that way anymore, but I forgot I had this posted here. Thanks @Mark-Amery for bringing to my attention.

var fakeList = [{ id: 1, name: "item 1" }, { id: 2, name: "item 2" }];

$http.expectGET('/myapi/myresource').respond(fakeList);

 myAngularService.myMethod();

 expect(myAngularService.listIsLoading).toBeTruthy();
 expect(myAngularService.listFinishedSuccessfully).toBeUndefined();

 $http.flush();

 expect(myAngularService.listIsLoading).toBeFalsy();
 expect(myAngularService.listFinishedSuccessfully).toBeTruthy();
 expect(myAngularService.myList).toEqual(fakeList);

这篇关于延迟与$回应httpBackend的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆