运行我的单元测试代码时遇到问题,在尝试运行测试时收到此错误消息:
dashboard.component.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { Router, ActivatedRoute } from "@angular/router";
import { MockAF } from "../../providers/mockAf";
import { AF } from "../../providers/af";
import { DashboardComponent } from './dashboard.component';
import { SortOnLikePipe } from '../sort-on-like.pipe'
import { EditMessageModalComponent } from '../edit-message-modal/edit-
message-modal.component';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/of';
import { MockModal } from '../edit-course-modal/mockEdit-course-
modal.component';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
let routerStub;
beforeEach(async(() => {
routerStub = {
navigate: jasmine.createSpy('navigate')
};
TestBed.configureTestingModule({
declarations: [
DashboardComponent,
SortOnLikePipe,
EditMessageModalComponent,
],
imports: [
FormsModule,
],
providers: [
{ provide: AF, useClass: MockAF },
{ provide: Router, useValue: routerStub },
{ provide: EditMessageModalComponent, useClass: MockModal },
{ provide: ActivatedRoute, useValue: { queryParams: Observable.from([{
course: "TDT4001" }]) }},
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
这是我在代码中使用管道的地方 的 dashboard.component.html
<div *ngFor="let message of afService.messages | async |
sortOnLike:'votes':false">
.....
</div>
dashboard.component.ts
import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild } from
'@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AF } from '../../providers/af';
import { FirebaseListObservable, AngularFire } from 'angularfire2';
import { Bubble } from './bubble';
import { EditMessageModalComponent } from '../edit-message-modal/edit-
message-modal.component';
@Component({
moduleId: module.id.toString(),
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit, AfterViewChecked {
constructor(public afService: AF, private router: Router, private route:
ActivatedRoute) {
}
ngOnInit(): void {
var sub = this.route
.queryParams
.subscribe(params => {
// Defaults to error if no query param provided.
this.afService.setCourse(params['course'] || "error");
});
}
我不知道如何解决这个问题,有趣的是这一切都奏效了。但当我回到家继续测试时,我得到了这个错误。所有帮助表示赞赏:)