我试图在一个非常简单的组件上运行一个基本的单元测试。
零件
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CmsService } from '../services/cms.service';
@Component({
selector: 'app-balance',
templateUrl: './balance.component.html',
styleUrls: ['./balance.component.css']
})
export class BalanceComponent implements OnInit {
data;
balance;
constructor(private router: Router, private service: CmsService) {
this.data = this.service.getData();
this.balance = this.data['balance'];
}
ngOnInit() {
}
}
规格文件
import { async, ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';
import { BalanceComponent } from './balance.component';
import { RouterTestingModule } from '@angular/router/testing';
import { CmsService } from '../services/cms.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HeaderComponent } from '../header/header.component';
import { FooterComponent } from '../footer/footer.component';
import { SidebarComponent } from '../sidebar/sidebar.component';
import { MessagesComponent } from '../messages/messages.component';
import { KeysPipe } from '../../../paysign-angular-common/lib/src/pipes/keys.pipe';
fdescribe('BalanceComponent', () => {
let component: BalanceComponent;
let fixture: ComponentFixture<BalanceComponent>;
let service: CmsService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BalanceComponent, HeaderComponent, FooterComponent, SidebarComponent, MessagesComponent, KeysPipe ],
providers: [ CmsService ],
imports: [ RouterTestingModule, HttpClientTestingModule ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BalanceComponent);
component = fixture.componentInstance;
service = TestBed.get(CmsService);
spyOn(service, 'getData').and.returnValue({balance: 50.00});
});
// it('should make a call to getData on load', async(() => {
// }));
it('should create with a balance avaliable in the session', () => {
fixture.detectChanges();
sessionStorage.setItem('data', JSON.stringify({balance: 50.00}));
spyOn(service, 'getData').and.callFake(() => {
return sessionStorage.getItem('data');
});
component.data = service.getData();
expect(component.balance).toEqual(50.00);
expect(component).toBeTruthy();
sessionStorage.clear();
});
});
主要问题是测试运行时,夹具未定义。堆栈跟踪是:
TypeError: Cannot read property 'detectChanges' of undefined
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/src/app/balance/balance.component.spec.ts:40:13)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone.js:388:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone-testing.js:288:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone.js:387:1)
at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone.js:138:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone-testing.js:509:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/C:/Users/aaleksandrov/Documents/Paysign-Kiosk-client/node_modules/zone.js/dist/zone-testing.js:524:1)
at attempt (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?73bc53d3671677e6a093fc74e4f9bcde57e5f7ad:4289:46)
at ZoneQueueRunner.QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?73bc53d3671677e6a093fc74e4f9bcde57e5f7ad:4217:20)
at runNext (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?73bc53d3671677e6a093fc74e4f9bcde57e5f7ad:4257:20)
如果我将夹具创建移到beforeEach之外,则不会发生这种情况。我已经尝试将其移动到异步块中,但我的想法已经用完了。
编辑:进一步研究表明TestBed本身在其调用的任何地方都给出了一个Reference错误。
我用你的代码试图帮助你调试,但我无法重现。我删除了与Fixture.detectChanges()的使用无关的所有元素。这是我运行和成功的代码。
import { async, ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';
import { BalanceComponent } from './balance.component';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
fdescribe('BalanceComponent', () => {
let component: BalanceComponent;
let fixture: ComponentFixture<BalanceComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BalanceComponent ],
imports: [ RouterTestingModule, HttpClientTestingModule ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BalanceComponent);
component = fixture.componentInstance;
});
// it('should make a call to getData on load', async(() => {
// }));
it('should create with a balance avaliable in the session', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
});
我的猜测是,你使用过时的Jasimne / Karma npm软件包可能会以不同方式声明Fixture。你可以发布你的package.json所以我可以看一下吗?