我正在尝试测试一个使用 inversify 的类用于依赖注入(inject)并使用 @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 | |
---------------------|----------|----------|----------|----------|-------------------|
使用的包版本:
- typescript 3.5.3
- 反转 5.0.1
- 开 Jest 24.9.0
- 反射(reflect)元数据 0.1.13
关于javascript - 测试装饰构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347101/