我有一個打字稿類 userInfo.ts:
export class UserInfo {
public getName() {
return "I am real name";
}
}
我在mocks檔案夾中有一個模擬類 userInfo.ts :
export class UserInfo {
public getName() {
return "I am fake name";
}
}
我有一個客戶:
import { UserInfo } from "./userInfo";
export class Client {
public functionToTest() {
let validation = new UserInfo();
return validation.getName();
}
}
最后,我想為此進行兩個測驗,在第一個測驗中,我只想為此測驗覆寫 getName 模擬,在第二個測驗中,我想要模擬類行為,因此:
import { Client } from "./client";
import { UserInfo } from "./userInfo";
jest.mock("./userInfo");
const userInfoMocked = UserInfo as jest.MockedClass<typeof UserInfo>; // I tried with this but with no success
describe("Client", () => {
it("should get Name", () => {
let client = new Client();
// UserInfo.prototype.getName = jest.fn().mockImplementationOnce(() => {
// return "Something weird happened";
// });
userInfoMocked.prototype.getName = jest.fn().mockImplementationOnce(() => {
return "something weird happend";
});
// this is not working either
// Property 'getName' does not exist on type 'MockedClass<typeof UserInfo>'.
// userInfoMocked.getName = jest.fn().mockImplementationOnce(() => {
// return "something weird happend";
// });
let text = client.functionToTest();
expect(text).toBe('something weird happend');
let text2 = client.functionToTest();
expect(text2).toBe('I am fake name'); // I get undefined (I overwrote prototype!)
});
it('should get fake name now', () => {
let client = new Client();
let text3 = client.functionToTest();
expect(text3).toBe('I am fake name'); // I get undefined
});
});
我很驚訝這種常見的(我認為)功能無法實作?如何在這方面取得成功?這甚至可能嗎?
uj5u.com熱心網友回復:
您可以為模擬分配默認實作:
import userInfo from "./userInfo"
jest.mock("./userInfo", () => ({
getName: jest.fn(() => 'John Doe'),
}));
每次你想覆寫實作時:
userInfo.getName.mockImplementation(() => jest.fn().mockReturnValue('another value'));
uj5u.com熱心網友回復:
如果你想在不同的測驗中使用不同的模擬,不要使用模擬檔案夾。而是為每個測驗創建您需要的模擬。這描述了您可以執行的不同型別的模擬。根據您的描述,我將使用 mockImplementation。例如在一項測驗中你可以做
UserInfo.mockImplementation(() => {
return {
getName: () => {
return 'I am a real name'
}
}
}
在另一個測驗中:
UserInfo.mockImplementation(() => {
return {
getName: () => {
return 'I am a fake name'
}
}
}
所有方法都歸結為同一件事,因此選擇最適合您的代碼結構并且易于維護的方法是一個問題。
uj5u.com熱心網友回復:
這是我的解決方案:當不使用手動模擬(因此在模擬下有 UserInfo.ts)時,它根本不起作用,但是當擺脫手動模擬并離開自動模擬時,我可以這樣做:
import { mocked } from "ts-jest";
import { Client } from "./secretsManagerWrapper";
import { UserInfo } from "./userInfo";
jest.mock("./userInfo");
const userInfoMocked = mocked(UserInfo, false);
describe("Client", () => {
it.only("should get Name", () => {
let client = new Client();
userInfoMocked.mockImplementationOnce(function () {
return {
getName: () => {
return "John Doe Second";
},
};
});
它奏效了!
我還發現,當我想使用手動模擬(下嘲笑檔案夾)我可以使用間諜:
describe("Client", () => {
it("should get Name", () => {
let client = new Client();
jest.spyOn(userInfoMocked.prototype, "getName").mockImplementationOnce(() => {
return "John Doe Second";
});
let text = client.functionToTest();
expect(text).toBe("John Doe Second");
let text2 = client.functionToTest();
expect(text2).toBe("I am fake name"); // I get what I want now
});
it("should get fake name now", () => {
let client = new Client();
let text3 = client.functionToTest();
expect(text3).toBe("I am fake name"); // I get what I want now !
});
});
我可以完成我想要的:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396427.html
標籤:javascript 打字稿 单元测试 玩笑 笑话
