简体   繁体   English

如何在Angular4中使用Karma-Jasmine测试导出功能

[英]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.

相关问题 Karma-Jasmine 如何在 angular 中测试一个路由器组件 - Karma-Jasmine How to test in angular a Router component 如何使用 Angular 中的 Karma-Jasmine 对嵌套的 if 条件进行单元测试? - How to unit test a nested if condition in with Karma-Jasmine in Angular? Karma-jasmine 我如何在模态中测试关闭的 function - Karma-jasmine How i test a close function in a modal Angular4-如何使用Karma和Jasmine对指令进行单元测试 - Angular4 - how to unit test a directive using Karma and Jasmine Angular 4 错误:Karma-Jasmine 测试中没有 ChildrenOutletContexts 的提供者 - Angular 4 Error: No provider for ChildrenOutletContexts in Karma-Jasmine Test Angular 2错误:在Karma-Jasmine测试中没有Http的提供者 - Angular 2 Error: No provider for Http in Karma-Jasmine Test 如何使用Angular2中的karma-jasmine对ng2-translate进行单元测试 - How to unit test ng2-translate using karma-jasmine in Angular2 Karma-Jasmine for.sort() 中的单元测试故障排除 Angular 中的 function - Unit Testing Troubleshoot in Karma-Jasmine for .sort() function in Angular 如何为动态配置文件 JSON 编写 karma-Jasmine 测试 - How to write a karma-Jasmine test for a dynamic configuration file JSON Karma-Jasmine单元测试语法 - Karma-Jasmine unit test syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM