【发布时间】:2016-12-23 17:54:34
【问题描述】:
我已经创建了这个测试:
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { GamePanelComponent } from './gamepanel.component';
describe('GamePanelComponent (inline template)', () => {
let comp: GamePanelComponent;
let fixture: ComponentFixture<GamePanelComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ GamePanelComponent ], // declare the test component
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(GamePanelComponent);
comp = fixture.componentInstance;
});
});
it('isValidMove', () => {
comp.ngOnInit();
comp.game.board[0][0] = 'X';
let isValid = comp.isValidMove(0,0);
expect(isValid).toBe(false);
});
});
奇怪的是测试失败了,因为TypeError: Cannot read property 'ngOnInit' of undefined 错误。
显然,comp 甚至 fixture 都没有定义。如您所见,我确实在 beforeEach 方法中初始化了它们:
fixture = TestBed.createComponent(GamePanelComponent);
comp = fixture.componentInstance;
我的代码有什么问题?
【问题讨论】: