angular - Angular 中 TestBed 的正确使用

标签 angular testbed

在一个基于 Angular 8.1.2 和 Ionic 4 的应用程序项目中,我为 typescript 中的一个简单类编写了单元测试。这与“npm test”配合得很好。为了准备需要模拟的更复杂的类,我更改了代码以使用 beforeEach 方法和 TestBed 对象:

import { CryptHelper, CryptCodeTyp, CryptIOStringTyp } from './cryptHelper';
import { TestBed } from '@angular/core/testing';

describe('CryptHelper', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ CryptHelper ],
      imports: [ ],
      providers: [ CryptHelper ],
    }).compileComponents();
  });

  it('should be created', () => {
    let fixture = TestBed.createComponent(CryptHelper);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeDefined();
  });
});

这样我就可以在测试输出中得到错误消息

error properties: Object({ ngSyntaxError: true })
Error: Unexpected value 'CryptHelper' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
    at syntaxError (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19889:1
    at <Jasmine>
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19871:1)
    at JitCompiler._loadModules (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25582:1)
    at JitCompiler._compileModuleAndAllComponents (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25571:1)
    at JitCompiler.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25533:1)
    at CompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:237:1)
    at TestingCompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:140:1)
    at TestBedViewEngine.compileComponents (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3118:1)

我发现建议从声明中删除类名,

...
    TestBed.configureTestingModule({
      declarations: [ ],
...

但这导致我看到此错误消息:

error properties: Object({ ngSyntaxError: true })
Error: Illegal state: Could not load the summary for directive CryptHelper.
    at syntaxError (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at CompileMetadataResolver.getDirectiveSummary (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19729:1)
    at JitCompiler.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25536:1)
    at CompilerImpl.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:263:30)
    at TestingCompilerImpl.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:148:1)
    at TestBedViewEngine.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3418:1)
    at Function.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3000:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/core/services/cryptHelper.spec.ts:15:27)
    at <Jasmine>

我发现了在声明中写入类名的建议。但这导致我看到上面的错误消息。那么我错了什么?欢迎任何帮助。

最佳答案

假设 CryptHelper 是一个组件:

    TestBed.configureTestingModule({
        declarations: [ CryptHelper], // Your testing component goes here
        providers: [ // Your component's providers goes here, for example a mocked service
            {
                provide: MyService, useValue: mockMyService
            }
        ],
        imports: [
            // imports from your component, for example MatDialogModule, MatInputModule, MyComponentModule, ...
        ]
    })
    .compileComponents();

假设 CryptHelper 是一个服务:

let cryptHelper: CryptHelper;

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [CryptHelper],
        imports: [] 
    });

    cryptHelper = TestBed.get(CryptHelper);
});

describe('CryptHelper Service creation', () => {
    it('should be created', inject([CryptHelper], (service: CryptHelper) => {
        expect(service).toBeTruthy();
    }));
});

关于angular - Angular 中 TestBed 的正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59303368/

相关文章:

angular - 'zxing-scanner' 不是已知元素 : 1. 如果 'zxing-scanner' 是 Angular 组件,则验证它是该模块的一部分

c++ - 如何让 Box2D 只创建一个实体实例?

testing - 试验台和模拟器之间的主要区别是什么?

angular - 异步测试确实遍历了所有功能

angular - 使用@ViewChild 对 Angular 5 组件进行单元测试

angular - 公司代理背后的 Protractor (Angular-CLI)

angular - 为什么将空字符串设置为空 react 形式成为空字符串

javascript - 将内部 `[(ngModel)]` 用于通用控件?

html - Angular :L 形边框

angular - 修改 TestBed.overrideComponent 中定义的组件