最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

诗乃:不能嘲笑诺言

IT培训 admin 4浏览 0评论

诗乃:不能嘲笑诺言

我知道这个问题很常见,我已经阅读了所有相关的问题,但似乎我仍然无法弄清楚如何将它们应用到我的案例中。

这是我的错误:

 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();
                });
        });
    });

我做错了什么?

回答如下:

好吧,我只是错过了最后调用done功能。

正确答案:

sinon.stub(Auth0Service.prototype, "signIn").resolves({
    success: false,
     accessToken: "test",
     errorMessage: "aaa",
     statusCode: 404,
} as SignInResultModel);
 done();

诗乃:不能嘲笑诺言

我知道这个问题很常见,我已经阅读了所有相关的问题,但似乎我仍然无法弄清楚如何将它们应用到我的案例中。

这是我的错误:

 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();
                });
        });
    });

我做错了什么?

回答如下:

好吧,我只是错过了最后调用done功能。

正确答案:

sinon.stub(Auth0Service.prototype, "signIn").resolves({
    success: false,
     accessToken: "test",
     errorMessage: "aaa",
     statusCode: 404,
} as SignInResultModel);
 done();

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论