我试图对基于express
框架并使用TypeScript
的API进行单元测试。
我使用sinon
和supertest
库。
这是我尝试存根的控制器:
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
如何对这种方法进行存根处理?