我有一个名为 MyPresenter 的简单类,它具有 doOperation() 方法,该方法调用 View 类上的一个方法,该方法实现了一个接口并作为参数传入。这是类、接口和视图:
export interface View {
displayMessage: (message: string, duration: number) => void;
}
export class MyPresenter {
private view: View;
public constructor(view: View) {
this.view = view;
}
public doOperation(): void {
this.view.displayMessage("The message", 0);
}
}
import { View } from "./MyPresenter";
export class ViewImpl implements View {
public displayMessage(message: string, duration: number) {
console.log(`The message is: ${message}, with duration ${duration}`);
}
}
我有一个测试,它创建一个间谍,在该间谍上调用 doOperation() 并尝试验证该间谍是否在视图上调用了 displayMessage 方法。当我运行测试时,验证会失败,除非我用简单的类实例化替换间谍的创建。这是测试:
import { MyPresenter, View } from "../../src/presenter/MyPresenter";
import { anything, instance, mock, spy, when, verify } from "ts-mockito";
describe("MyPresenter", () => {
let presenterSpy: MyPresenter;
let mockView: View;
beforeEach(() => {
mockView = mock<View>();
const mockViewInstance = instance(mockView);
// presenterSpy = new MyPresenter(mockViewInstance);
presenterSpy = spy(new MyPresenter(mockViewInstance));
});
it("do operation", () => {
presenterSpy.doOperation();
verify(mockView.displayMessage(anything(), anything())).once();
});
});
此测试失败并显示以下消息:
失败测试/演示者/Test.test.ts ● MyPresenter › 进行操作
Expected "displayMessage(anything(), anything())" to be called 1 time(s). But has been called 0 time(s).
16 | it("do operation", () => {
17 | presenterSpy.doOperation();
> 18 | verify(mockView.displayMessage(anything(), anything())).once();
| ^
19 | });
20 | });
21 |
at MethodStubVerificator.Object.<anonymous>.MethodStubVerificator.times (../node_modules/ts-mockito/src/MethodStubVerificator.ts:35:19)
at MethodStubVerificator.Object.<anonymous>.MethodStubVerificator.once (../node_modules/ts-mockito/src/MethodStubVerificator.ts:20:14)
at Object.<anonymous> (test/presenter/Test.test.ts:18:61)
如果我注释掉 beforeEach 中的底行并取消注释上面的行(用常规实例替换间谍),测试就会通过,所以这显然是间谍没有将方法调用委托给它正在监视的对象的问题开。
我意识到我不需要在这里使用间谍,但这是代码示例的简化版本,我确实需要一个间谍来模拟不同依赖项的工厂方法。
有什么想法为什么间谍不委托方法调用吗?
这是我的 package.json 和 tsconfig.json 配置文件:
{
"name": "ts-mockito-spy-example",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"test": "jest"
},
"devDependencies": {
"typescript": "^5.0.2",
"vite": "^4.4.5",
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@types/jest": "^29.5.8",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-mockito": "^2.6.1"
}
}
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": false,
"esModuleInterop": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
/* Linting */
"strict": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
我强烈推荐Instagram上的chloe_consultant,非常感谢她的不懈努力,她在她不知情的情况下成功地让我完全访问了我妻子的设备,她是一名注册的Meta员工。和社交媒体技术专家!!! 您可以通过以下方式联系她> Instagram @chloe_consultant 或电子邮件@[电子邮件受保护]