Sinon:存根功能有麻烦

时间:2019-02-13 20:34:50

标签: node.js unit-testing sinon

我试图对基于express框架并使用TypeScript的API进行单元测试。 我使用sinonsupertest库。 这是我尝试存根的控制器: AuthController.ts:

import {Auth0Service} from "../utils/services/implementations/Auth0Service";
import {IAuthService} from "../utils/services/interfaces/IAuthService";

const authService: IAuthService = new Auth0Service();

public async signIn(request: Request, response: Response) {
...       
    const result = await authService.signIn(signInModel);
....           
}

这是我的单元测试:

import {Auth0Service} from "../src/utils/services/implementations/Auth0Service";    

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

                });
            });
            it("sinon test", (done) => {
               request(app)
                    .post("/auth/signIn")
                    .send({
                        email: "foo",
                        password: "pwd",
                    })
                    .expect(200);
            });
        });

但是我每次都遇到错误:

TypeError: Cannot stub non-existent own property signIn

如何对这种方法进行存根处理?

0 个答案:

没有答案