这是我的第一个问题,希望能做好...
我是一个在 Angular 中使用 Karma-Jasmine 进行单元测试的初学者,我刚刚发现一个我不知道如何解决的案例。
我有一个如下所示的 .ts 文件:
example.constants.ts
此文件具有生成随机 ID 的功能。这是我的代码:
export function generateUid(separator: string) {
...
}
我正在尝试对此功能进行测试,因为我需要覆盖它。所以我决定创建一个文件example.constants.spec.ts。看起来像这样
import { TestBed } from '@angular/core/testing';
import { generateUid } from './example.constants';
describe('ExampleConstants generateUid', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: []
});
});
it('should check if Uid is generated',
() => {
expect(0).toBe(0);
});
});
问题不在于功能正常时如何覆盖。问题是当我运行 ng test --code-coverage 时,这个测试没有出现。我一直在使用组件和服务单元测试,但这是我第一次想对导出功能进行测试。此功能没有关联的组件。它像导出函数一样在 example.constants.ts 中声明。
你能帮我做一个关于导出函数的单元测试吗?
问候。
最佳答案
您不需要为此场景配置测试模块。您可以像正常功能一样测试它
import { TestBed } from '@angular/core/testing';
import { generateUid } from './example.constants';
describe('ExampleConstants generateUid', () => {
it('should check if Uid is generated',
() => {
generateUid('-').toBeDefined();
});
});
关于angular - 如何在 Angular4 中使用 Karma-Jasmine 测试导出功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287174/