[英]How to test export function with Karma-Jasmine in Angular4
It is my first question here, I hope to do well... 这是我的第一个问题,我希望做得好...
I am a begginer doing unit tests with Karma-Jasmine in Angular and I just found a case that I do not know how to resolve it. 我是一个乞with,正在Angular中与Karma-Jasmine进行单元测试,我发现一个案例,我不知道如何解决。
I have a .ts file that looks like this: 我有一个.ts文件,看起来像这样:
example.constants.ts example.constants.ts
This file have a function that generate a random ID. 该文件具有生成随机ID的功能。 Here is my code:
这是我的代码:
export function generateUid(separator: string) {
...
}
I am trying to do a test of this function because i need cover it. 我正在尝试对此功能进行测试,因为我需要掩盖它。 So i decided to create a file example.constants.spec.ts .
因此,我决定创建一个文件example.constants.spec.ts 。 It looks like this
看起来像这样
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);
});
});
The problem is not about how to cover if the function work well. 问题不在于该功能是否正常工作。 The problem is about this test does not appear when I run ng test --code-coverage .
问题是我运行ng test --code-coverage时没有出现此测试。 I have been working with components and services unit tests but it is the first time that i want to do a test about an export function.
我一直在进行组件和服务单元测试,但这是我第一次要对导出功能进行测试。 This function not have associated component.
该功能没有关联的组件。 It is declare in example.constants.ts like an export function.
它在example.constants.ts中像导出函数一样声明。
Could you help me to do this unit test about an export function? 您能帮我做一下有关导出功能的单元测试吗?
Regards. 问候。
You don't need to configure testing modules for this scenario. 您无需为此场景配置测试模块。 You can test it just like normal functions
您可以像正常功能一样对其进行测试
import { TestBed } from '@angular/core/testing';
import { generateUid } from './example.constants';
describe('ExampleConstants generateUid', () => {
it('should check if Uid is generated',
() => {
generateUid('-').toBeDefined();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.