Javascript Spec没有期望-Jasmine测试回调函数
我有一个使用Javascript Spec没有期望-Jasmine测试回调函数,javascript,angular,typescript,jasmine,jasmine2.0,Javascript,Angular,Typescript,Jasmine,Jasmine2.0,我有一个使用d3定时器调用的方法。无论何时调用该方法,该方法都会发出一个包含两个值的对象。 其中一个值随时间增加。我想写一个测试来检查这些值是否按升序排列(即,是否随时间增加) 因此,为了解决这个问题,在我的测试中,我订阅了事件发射器,在订阅中,我将接收到的对象推送到一个本地数组中。然后,我希望数组[I]小于数组[I+1]。我认为我的逻辑是完全正确的,但我不确定为什么我会从Jasmine那里得到一个错误,说规范没有期望值,即使我有一个期望值 代码如下: let x = d3.timer((ela
d3定时器调用的方法。无论何时调用该方法,该方法都会发出一个包含两个值的对象。
其中一个值随时间增加。我想写一个测试来检查这些值是否按升序排列(即,是否随时间增加)
因此,为了解决这个问题,在我的测试中,我订阅了事件发射器,在订阅中,我将接收到的对象推送到一个本地数组中。然后,我希望数组[I]
小于数组[I+1]
。我认为我的逻辑是完全正确的,但我不确定为什么我会从Jasmine那里得到一个错误,说规范没有期望值,即使我有一个期望值
代码如下:
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 });
}
}
茉莉花规格:
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);
}
});
}));
it('my spec',inject([JumpService],(service:JumpService)=>{
数组=[];
//服务调用该方法
service.output.subscribe(e=>{
array.push(e);
//这里的console语句将给出长度和所推的对象。
for(设i=0;i
我做错什么了吗?我如何处理这种情况?请把我引向正确的方向
谢谢。通常,在测试异步回调函数时,在承诺得到解决之后,期望测试的输出总是很重要的。您可以将Angular test bed框架的tick()
与fakeAsync()
结合使用,也可以简单地退回到Jasmine使用done()
测试异步方法的一般方法
使用done()
:
it('my spec',(done)=>{
数组=[];
service.output.subscribe(e=>{
array.push(e);
for(设i=0;i
希望这个答案有帮助。
注意:我在fakeAsync()
和tick()
方面运气不好,所以我没有在答案中包含它。很抱歉。尝试使用@angular/core/testing
中的async
函数。它
在异步测试区域中包装测试函数。这项测试将
当此区域内的所有异步调用发生时自动完成
完成了。可用于包装{@link inject}调用
请查找下面的代码示例:
it('...', async(inject([AClass], (object) => {
object.doSomething.then(() => {
expect(...);
})
});
您应该在承诺的末尾使用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')
})
)
希望这对您有所帮助我成功地使用waitForAsync来包装我的it功能
it('should display correct data', waitForAsync(() => {
fixture.whenStable().then(() => {
// expect
});
}));
只是相关;我得到这个错误是因为我有一个额外的arrow func指示符,例如,it('doesthings',()=>()=>{
it('should display correct data', waitForAsync(() => {
fixture.whenStable().then(() => {
// expect
});
}));