我有一堂課應該記錄一些東西。記錄器是這樣定義的。
export class Logger {
public static client = ClientStrategy.create();
public async logSth(msg: string) {
params = {"testThing": msg};
await Logger.client.updateItem(params);
}
export class ClientStrategy {
public static create() {
return new DatabaseClient(); // not important here just a factory, with a method updateItem
}
}
我試圖在一個開玩笑的測驗中模擬這個。我正在嘗試模擬這個客戶端策略和一個創建方法。但它沒有像我預期的那樣作業。
jest.mock("./ClientStrategy");
const mockClientStrategy = jest.mocked(ClientStrategy, false);
describe("Logger", () => {
beforeEach(() => {
jest.spyOn(<any>mockDatabaseClientStrategy, "create").mockImplementation(() => {
return {
updateItem: () => {
return {}
}
};
});
});
it("should log sth", async () => {
const response = await Logger.logSth("testMsg");
expect(response).not.tobeUndefined();
});
});
但我得到無法讀取未定義的屬性“logSth”。你能幫我在這里做錯什么嗎?我認為我的模擬應該可以正常作業,但不幸的是我對此沒有定義。
uj5u.com熱心網友回復:
兩者都jest.mock()將jest.spyOn()起作用。但是對于您的情況,無需將它們一起使用。因此,讓我們使用jest.spyOn()來解決您的問題。
由于ClientStrategy.create將在您匯入Logger類時執行,因此您需要ClientStrategy.create在匯入Logger類之前添加 spy on。
此外,該logSth方法是實體方法,而不是類方法。
例如
Logger.ts:
import { ClientStrategy } from './ClientStrategy';
console.log('ClientStrategy.create: ', ClientStrategy.create);
export class Logger {
public static client = ClientStrategy.create();
public async logSth(msg: string) {
const params = { testThing: msg };
return Logger.client.updateItem(params);
}
}
Logger.test.ts:
import { ClientStrategy } from './ClientStrategy';
// import { Logger } from './Logger';
describe('Logger', () => {
let mockClient;
let Logger: typeof import('./Logger').Logger;
beforeEach(async () => {
mockClient = {
updateItem: jest.fn(),
};
jest.spyOn(ClientStrategy, 'create').mockReturnValue(mockClient);
Logger = await import('./Logger').then((m) => m.Logger);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should log sth', async () => {
mockClient.updateItem.mockResolvedValueOnce('fake value');
const logger = new Logger();
const response = await logger.logSth('testMsg');
expect(response).toEqual('fake value');
});
});
ClientStrategy.ts:
export class ClientStrategy {
public static create() {
return { updateItem: async (params) => 'real value' };
}
}
測驗結果:
PASS stackoverflow/71938329/Logger.test.ts
Logger
? should log sth (22 ms)
console.log
ClientStrategy.create: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function (anonymous)],
mock: [Getter/Setter],
mockClear: [Function (anonymous)],
mockReset: [Function (anonymous)],
mockRestore: [Function (anonymous)],
mockReturnValueOnce: [Function (anonymous)],
mockResolvedValueOnce: [Function (anonymous)],
mockRejectedValueOnce: [Function (anonymous)],
mockReturnValue: [Function (anonymous)],
mockResolvedValue: [Function (anonymous)],
mockRejectedValue: [Function (anonymous)],
mockImplementationOnce: [Function (anonymous)],
mockImplementation: [Function (anonymous)],
mockReturnThis: [Function (anonymous)],
mockName: [Function (anonymous)],
getMockName: [Function (anonymous)]
}
at Object.<anonymous> (stackoverflow/71938329/Logger.ts:3:9)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.57 s, estimated 11 s
您可以取消注釋該import { Logger } from './Logger';陳述句并注釋import()以檢查日志。您將看到未模擬的ClientStrategy.create方法:
console.log
ClientStrategy.create: [Function: create]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/460502.html
標籤:javascript 单元测试 开玩笑的 嘲弄 开玩笑
上一篇:開玩笑的測驗用例反應失敗
