[英]Testing decorated constructor parameters
我正在嘗試測試使用inversify進行依賴項注入並使用@inject()
修飾參數的類。
@injectable()
export class SaveManager {
constructor(
@inject(INJECTABLE.STORAGE) storage: IStorage, //Uncovered Line: 15
@inject(Configuration) configuration: Configuration //Uncovered Line: 16
) {
this.storage = storage;
this.configuration = configuration;
}
}
但是此類的分支覆蓋范圍向我展示了:
@inject(INJECTABLE.STORAGE) storage: IStorage, //Uncovered Line: 15
@inject(Configuration) configuration: Configuration //Uncovered Line: 16
這些行不包括在內。
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line
SaveManager.ts | 100 | 66.67 | 100 | 100 | 15,16
我應該在這里測試什么才能達到100%的覆蓋率,以及如何測試構造函數參數裝飾器?
我知道我可以使用container.get()
但我也應該能夠測試裝飾器。
使用當前版本的軟件包依賴關系,代碼覆蓋率很好 。 您可以使用下面的任何一種測試,您將獲得100%的SaveManager
覆蓋率。
使用InversifyJS解析實例:
test('Resolve SaveManager', () => {
const manager = myContainer.get(SaveManager);
expect(manager).not.toBe(null);
});
直接創建實例:
test('Create new SaveManager', () => {
const storage = myContainer.get<IStorage>(INJECTABLE.STORAGE);
const config = myContainer.get<Configuration>(Configuration);
const manager = new SaveManager(storage, config);
expect(manager).not.toBe(null);
});
兩種情況下的結果覆蓋率:
PASS tests/save-manager.test.ts
---------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------------|----------|----------|----------|----------|-------------------|
...
save-manager.ts | 100 | 100 | 100 | 100 | |
---------------------|----------|----------|----------|----------|-------------------|
使用的軟件包版本:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.