诗乃:不能嘲笑诺言

时间:2019-02-13 22:08:45

标签: node.js typescript unit-testing sinon

我知道这个问题很常见,我已经阅读了所有相关问题,但是似乎我仍然不知道如何将其应用于我的案子。

这是我的错误:

 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我试图对具有下一个签名的方法进行存根:

 public async signIn(model: SignInInputModel): Promise<SignInResultModel> {...}

在单元测试开始之前,我不会存根响应模型。 这是我尝试不成功的方法:

describe("incorrect behaviour", () => {
        before((done) => {
            sinon.stub(Auth0Service.prototype, "signIn").resolves(() => {
                const result = new SignInResultModel();
                result.success = false;
                result.errorMessage = "foo";
                return result;
            });

            //this not working as well
            //  sinon.stub(Auth0Service.prototype, "signIn").returns(() => {
            //     const result = new SignInResultModel();
            //     result.success = false;
            //     result.errorMessage = "foo";
            //     Promise.resolve(result);
            // });

        });
        it("sinon test", async (done) => {
            request(app)
                .post("/auth/signIn")
                .send()
                .expect(200)
                .end((err, res) => {
                    ...
                    done();
                });
        });
    });

我做错了什么?

2 个答案:

答案 0 :(得分:0)

好吧,我只是错过了最后调用BROWSERSTACK_NO_LOCAL="TRUE"函数的地方。

正确答案:

done

答案 1 :(得分:0)

resolves()应该返回一个值,您正在返回一个函数,这似乎是错误的。

let result = new SignInResultModel();
result.success = false;
result.errorMessage = "foo";
sinon.stub(Auth0Service.prototype, "signIn").resolves(result);