【问题标题】:Got error (timeout of 2000ms exceeded) while testing async function in a service在服务中测试异步功能时出错(超过 2000 毫秒的超时)
【发布时间】:2017-01-20 00:37:20
【问题描述】:

我有以下详细信息的服务:

class MyService {
   myFunction(param){
        return Observable.create(obs => {
             callsDBfunc(param, (err, res) => {
                  if(err)
                      obs.error(err);
                  else
                      obs.next(res);
             });
        });
   }
}

当我尝试测试它时,我收到一个错误:

错误:超过 2000 毫秒的超时。确保 done() 回调正在执行 在此测试中调用。

我的测试如下:

it('test signup()', (done) => {
     authService = new AuthService();
     let credentials = {username: '1234', password: '1234'};
     let spyVMAddUser = spy(VM_Users_AddUser, 'call');
     authService.signup(credentials).subscribe(data => {
         expect(data).to.eql(credentials);
         done();
     });
     assert.calledOnce(spyVMAddUser);
     spyVMAddUser.restore();
});

我尝试 (1) 将 observale 转换为 Promise,然后像 .then(done,done) 一样将 done 链接到末尾。 (2) 尝试将 setTimeOut(20000) 设置为同样的错误。

(3) 我什至尝试 stub 方法而不是使用以下代码 spy 并得到相同的错误。

stub(VM_Users_AddUser, 'call').withArgs(credentials).returns(credentials);

请帮忙。我正在尝试解决过去 2-3 天的此错误。

【问题讨论】:

    标签: angular jasmine mocha.js sinon


    【解决方案1】:

    发现问题。我没有通过适当的回调调用函数。当我用 yieldAaync(null, callbackSpy) 调用存根时,它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-13
      • 2017-04-15
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2015-02-05
      • 2020-10-23
      • 2019-04-14
      相关资源
      最近更新 更多