typescript 规范没有期望 - Jasmine 测试回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45578981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spec has no expectations - Jasmine testing the callback function
提问by zelda
I have a method which is being called using a d3 timer
. Whenever the method is called, the method emits an object with a couple of values.
One of the values increases over time. I would like to write a test to check whether the values are in the ascending order or not (i.e., increasing over time or not).
我有一个使用d3 timer
. 每当调用该方法时,该方法都会发出一个具有几个值的对象。其中一个值随时间增加。我想编写一个测试来检查值是否按升序排列(即,是否随时间增加)。
So, to tackle this, In my test, I subscribe to the event emitter and inside the subscription, I am pushing the object which I receive into a local array. And then, I am expecting the array[i]
to be less than the array[i+1]
. I think my logic is perfectly correct but I am not sure why I am getting an error from Jasmine saying that the spec has no expectations
even though I have one.
因此,为了解决这个问题,在我的测试中,我订阅了事件发射器,并且在订阅内部,我将接收到的对象推送到本地数组中。然后,我希望array[i]
小于array[i+1]
。我认为我的逻辑是完全正确的,但我不知道为什么我会从 Jasmine 那里得到一个错误,the spec has no expectations
即使我有一个错误。
Here is the code:
这是代码:
let x = d3.timer((elapsed) => {
this.method(); // call the function
if(elapsed >= 500) {
x.stop(); // stops the timer.
}
});
method(elapsed) {
// do something
if(elapsed > 500) {
this.output.emit({x: somevalue, y: somevalue, f: increasingvalue });
}
}
The Jasmine Spec:
茉莉花规格:
it('my spec', inject([JumpService], (service: JumpService) =>{
array = [];
//service calls the method
service.output.subscribe(e => {
array.push(e);
// A console statement here will give me the length and the object pushed.
for(let i = 0; i< array.length - 1; i++) {
expect(array[i].f).toBeLessThan(array[i+1].f);
}
});
}));
Am I doing anything wrong here? How can I tackle such kind of a scenario? Please guide me in a right direction.
我在这里做错了什么吗?我该如何处理这种情况?请指导我正确的方向。
Thank you.
谢谢你。
采纳答案by ShellZero
In general, when testing the async callback functions, it is always important to expect the outputs of the test after the promises are resolved. You can use the Angular test bed framework's tick()
with the fakeAsync()
or you can simply fallback to the Jasmine's general way of testing the async methods by using done()
一般来说,在测试异步回调函数时,在承诺解决后期待测试的输出总是很重要的。您可以使用 Angular 测试平台框架tick()
,fakeAsync()
或者您可以简单地使用 Jasmine 测试异步方法的一般方法done()
Using done()
:
使用done()
:
it('my spec', (done) => {
array = [];
service.output.subscribe(e => {
array.push(e);
for(let i = 0; i< array.length - 1; i++) {
expect(array[i].f).toBeLessThan(array[i+1].f);
}
done();
});
});
Hope this answer helps.
希望这个答案有帮助。
Note: I didn't had great luck with the fakeAsync()
and tick()
, so I am not including it in the answer. Sorry about that.
注意:我对fakeAsync()
and 的运气不太好tick()
,所以我没有将它包含在答案中。对于那个很抱歉。
回答by Aliaksei Maniuk
Try using async
function from @angular/core/testing
. It
尝试使用async
来自@angular/core/testing
. 它
Wraps a test function in an asynchronous test zone. The test will automatically complete when all asynchronous calls within this zone are done. Can be used to wrap an {@link inject} call.
将测试功能包装在异步测试区中。当该区域内的所有异步调用完成后,测试将自动完成。可用于包装 {@link 注入} 调用。
Please find code example below:
请在下面找到代码示例:
it('...', async(inject([AClass], (object) => {
object.doSomething.then(() => {
expect(...);
})
});
回答by Cristian Zumelzu
You should use done() at the end of the promise, but from Jasmine 2.8.0 that will not work because there is not implementation for a done() method. You should test your promises like:
您应该在承诺的末尾使用 done() ,但是从 Jasmine 2.8.0 开始,这将不起作用,因为没有 done() 方法的实现。您应该测试您的承诺,例如:
it('does test promise',
inject([MyService], async (myService: MyService) => {
const result = await myService.serviceToTest()
expect(result).not.toBeNull()
expect(result).toBe('Some Value')
})
)
Hope that this help you
希望这对你有帮助