所有测试都给出相同的失败结果,但为什么呢?



我已经通过Angular CLI创建了几个组件和服务。 规范文件是自动生成的。

当运行我的测试(其中 56 个)时,它们都失败并显示相同的消息两次:

失败:模块声明的意外值"[对象对象]" "动态测试模块">

我明白为什么是两次,因为我之前有两个方法被调用。删除之前每个错误也会删除其中一个错误。我听到你说:"然后删除两个'之前每个'方法"。然后我的测试将不再运行。

我现在已经花了 2 天时间看这个,我完全没有想法。

我能找到的解决这个问题的唯一答案是我需要将我的路由文件导入到规范文件中。这样做了,但结果仍然是一样的。

使用 Angular 8 的全新安装。

我还尝试使用间谍来创建QueueComponent,因为它的构造函数中需要一个QueueService。同样,相同的结果。

其中一个规范文件如下所示:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { QueuesComponent } from './queues.component';
import { faCheckCircle } from '@fortawesome/free-solid-svg-icons';
import { faTimesCircle } from '@fortawesome/free-solid-svg-icons';
import {Queue} from '../../objects/queue';
import {environment} from '../../../environments/environment';
import {AppRoutingModule} from './../../app-routing.module';
describe('QueuesComponent', () => {
let component: QueuesComponent;
let fixture: ComponentFixture<QueuesComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [faCheckCircle, faTimesCircle, Queue, environment ],
imports: [AppRoutingModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(QueuesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

属于此等级库文件的组件是:

import {Component, OnDestroy, OnInit} from '@angular/core';
import {QueueService} from '../../services/queue.service';
import {Queue} from '../../objects/queue';
@Component({
selector: 'app-queues',
templateUrl: './queues.component.html',
styleUrls: ['./queues.component.css']
})
export class QueuesComponent implements OnInit, OnDestroy {
failureQueues: Queue[];
monitorQueues: Queue[];
constructor(private queueService: QueueService) {
}
ngOnInit() {
}
ngOnDestroy() {
}
}

HTML 文件只是在标记中显示标题。

我希望测试运行正常,因为我没有做任何花哨的事情。至少在我看来。

完整的错误消息是:

QueuesComponent > should create
Failed: Unexpected value '[object Object]' declared by the module 'DynamicTestModule'
error properties: Object({ ngSyntaxError: true })
Error: Unexpected value '[object Object]' declared by the module 'DynamicTestModule'
at syntaxError (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:19694:1
at <Jasmine>
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:19692:1)
at JitCompiler._loadModules (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25402:1)
at JitCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25391:1)
at JitCompiler.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25353:1)
at CompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:237:1)
at TestingCompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:140:1)
at TestBedViewEngine.compileComponents (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/testing.js:3086:1)

Error: Unexpected value '[object Object]' declared by the module 'DynamicTestModule'
error properties: Object({ ngSyntaxError: true })
Error: Unexpected value '[object Object]' declared by the module 'DynamicTestModule'
at syntaxError (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
at http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:19694:1
at <Jasmine>
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:19692:1)
at JitCompiler._loadModules (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25402:1)
at JitCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25391:1)
at JitCompiler.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25350:1)
at CompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:225:1)
at TestingCompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:132:1)
at TestBedViewEngine._initIfNeeded (http://localhost:9877/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/testing.js:3108:1)

Expected undefined to be truthy.
Error: Expected undefined to be truthy.
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9877/_karma_webpack_/webpack:/src/app/components/queues/queues.component.spec.ts:29:23)
at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.onInvoke (http://localhost:9877/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)

正如Erbsenkoenig所建议的那样,我删除了所有不必要的代码,并为QueueService创建了一个存根(而QueueService又使用HttpClient)。

import {async, ComponentFixture, getTestBed, TestBed} from '@angular/core/testing';
import { QueuesComponent } from './queues.component';
import {QueueService} from '../../services/queue.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
const dummyQueues = [
{value: 33, name: 'MDM03', max: 300},
{value: 0, name: 'MDM99', max: 200}
];
class StubQueueService {
getFailureQueues() {
return Observable.of(dummyQueues);
}
getMonitorQueues() {
return Observable.of(dummyQueues);
}
}
describe('QueuesComponent', () => {
let component: QueuesComponent;
let fixture: ComponentFixture<QueuesComponent>;
beforeEach(async(() => {
let injector;
let queuesService: QueueService;
TestBed.configureTestingModule({
declarations: [ QueuesComponent ],
imports: [ HttpClientTestingModule ],
providers: [
{provide: QueueService, useClass: StubQueueService}
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
}).compileComponents();
injector = getTestBed();
queuesService = injector.get(QueueService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(QueuesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

最新更新