AngularJS:测试范围$方法,使异步调用 [英] AngularJS: test $scope methods that make async calls
问题描述
我想测试一个控制器方法我在$范围。这方法使一个异步调用,并得到一个承诺回来。当许解决了,结果被分配给在$作用域的变量。
我试图想出测试很简单:null分配到$范围变量,调用该方法,等待调用完成,预计结果将分配给$范围的变量。
由于某些原因,承诺永远不会消失。请参见 http://plnkr.co/edit/qxs5Y54NvhA24Fk4MPv2?p=$p$pview
下面是控制器的方法:
$ scope.getSomething = - >
someFactory.find()然后(响应) - 过夜。;
$ scope.newValues = response.data
现在这就是我正在尝试对其进行测试:
它应该得到的东西, - >
scope.newValues = {}
scope.getSomething()
期待(scope.newValues.someData).toBeDefined()
有关单元测试的目的,我嘲笑someFactory使用的,据我相信,当传递的对象,应该立即解决$ q.when()方法。真的没有需要在这里等待。这是模拟工厂:
mockSomeFactory =($ Q) - GT;
{
发现: - >
$ {q.when someData:真正}
}
和使用$提供其注入到控制器:
beforeEach模块'myAngularApp',($提供) - GT;
$ provide.factorysomeFactory,mockSomeFactory
返回
我试图用茉莉花的运行,等待和waitsFor方法,但没有运气。 WaitsFor总是失败,即使经过5000毫秒。请参见 http://plnkr.co/edit/s2OL56jVCRK0Ecyycoz0?p=$p$pview
任何人都有在此之前发生的?
角承诺仅在消化周期中解决。
如果你调用$ rootScope.apply()的承诺应该解决的问题。
根。$适用()
期待(scope.newValues.data).toBeDefined()
您还需要改变一个变量的名称:我更新了code的 http://plnkr.co/edit/dB6zTpZ1jYXOO1kGAYvb?p=$p$pview
使用的数据是consitant。
$ scope.newValues.data = response.data#模拟工厂
mockSomeFactory =($ Q) - GT;
{
发现: - >
$ {q.when数据:真正}
}
希望这有助于
I'm trying to test a controller method I have on the $scope. That method makes an async call and get a promise back. When the promise is resolved, the result is assign to a variable on the $scope.
The test I'm trying to come up with is simple: assign null to the $scope variable, call the method, wait for the call to finish, expect the result to be assign to the $scope variable.
For some reason, the promise never resolves. See http://plnkr.co/edit/qxs5Y54NvhA24Fk4MPv2?p=preview
Here is the controller method:
$scope.getSomething = ->
someFactory.find().then (response) ->
$scope.newValues = response.data
Now this is how I'm trying to test it:
it 'should get something', ->
scope.newValues = {}
scope.getSomething()
expect(scope.newValues.someData).toBeDefined()
For the purpose of the unit test, I'm mocking "someFactory" using the $q.when() method which, as far as I believe, should resolve immediately when passed an object. There's really no wait needed here. This is the mock factory:
mockSomeFactory = ($q) ->
{
find: ->
$q.when {someData: true}
}
And injecting it into the controller using $provide:
beforeEach module 'myAngularApp', ($provide) ->
$provide.factory "someFactory", mockSomeFactory
return
I tried to use Jasmine's runs, waits and waitsFor methods but no luck. WaitsFor would always fail, even after 5000 milliseconds. See http://plnkr.co/edit/s2OL56jVCRK0Ecyycoz0?p=preview
Anyone had this happen before?
angular promises are only resolved during the digest cycle. if you call $rootScope.apply() the promise should resolve.
root.$apply()
expect(scope.newValues.data).toBeDefined()
You also needed to change the name of a variable: I updated the code http://plnkr.co/edit/dB6zTpZ1jYXOO1kGAYvb?p=preview
Using data to be consitant.
$scope.newValues.data = response.data
# mock factory
mockSomeFactory = ($q) ->
{
find: ->
$q.when {data: true}
}
Hope this helps
这篇关于AngularJS:测试范围$方法,使异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!