【发布时间】:2020-03-20 17:56:15
【问题描述】:
我想通过 ts-mockito@2.5.0 创建一个类的模拟对象,但我无法正确设置它。
这是人为的测试用例:
import {expect} from "chai";
import {
mock,
when,
} from "ts-mockito";
class MockMe {
public doStuff(): string {
return "I AM THE ORIGINAL VALUE";
}
}
describe("ts-mockito weirdness", async () => {
it("should create a mock with specific return values", async () => {
const mocked = mock(MockMe);
await when(mocked.doStuff()).thenReturn("I AM MOCKED");
const actualReturnValue = mocked.doStuff();
expect(actualReturnValue).to.eq("I AM MOCKED");
});
});
正如测试用例所暗示的,我希望我的模拟返回值“我被嘲笑”。
但我得到的是一个 ts-mockito-specifc 对象,其中包含以下属性:methodStubCollection、matchers、mocker 和 name。
我应该如何设置模拟以使其按预期工作?
旁注:这个测试用例只是为了展示我正在经历的奇怪行为。这不是我的实际测试。我想在不同服务的单元测试中使用模拟。)
【问题讨论】:
-
我打开了一个问题,要求尽可能改进错误消息:github.com/NagRock/ts-mockito/issues/169
标签: typescript unit-testing mocking ts-mockito