遇到这个问题并在互联网上搜索fixture.detectChanges(),当显式插入模拟数据时,它无法识别@Input()中的更改。有大量的线程和文档描述了设置,但不一定是为什么它会导致我的所有测试都破坏。
错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后发生了变化。
删除fixture.detectChanges()似乎"解决"这个错误。但是现在没有检测到任何新的模拟数据(按规格)的插入。
示例:
TestComponent.ts
import { TestComponent } from './test-component';
import { TestComponentModule } from './test-component.module';
import { Data } from './interfaces/data';
export class TestComponent {
@Input() data: Data;
displayData(){
let firstName = data.first;
let lastName = data.last;
let fullName = firstName + ' ' + lastName;
return fullName;
};
}
TestComponent.spec.ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestComponent } from './test-component';
import { TestComponentModule } from './test-component.module';
class DataMock {
data: Data = getDataMock({
first: 'Roger',
last: 'Moore'
});
};
describe('TestComponent', () => {
'use strict';
let testComponent: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async() => {
TestBed.configureTestingModule({
imports: [ TestComponentModule ]
}).compileComponents();
fixture = TestBed.createComponent(TestComponent);
testComponent = fixture.componentInstance;
fixture.detectChanges();
});
it('should render the app', () => {
expect(TestComponent).toBeDefined();
});
describe('displayData()', () => {
let dataMock = new DataMock;
beforeEach(() => {
testComponent.data = dataMock.data;
});
it('should return fullName', () => {
expect(TestComponent.displayData()).toBe('Roger Moore');
});
});
});
那么,为什么在fixture.detectChanges()工作所需的每个规范之前实例化类dataMock?这是什么原因?
答案 0 :(得分:2)
您必须在执行compileComponents之后创建固定装置。
beforeEach(async() => {
TestBed.configureTestingModule({
imports: [ TestComponentModule ]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
testComponent = fixture.componentInstance;
fixture.detectChanges();
});