注入依赖和DOM更改 - 覆盖测试的组件
优质
小牛编辑
130浏览
2023-12-01
overrideComponent
overrideDirective
overridePipe
import {MessageComponent} from './message.component';
import { provide } from '@angular/core';
import {
async,
inject,
TestBed,
} from '@angular/core/testing';
describe('MessageComponent', () => {
let fixture;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MessageComponent],
providers: []
});
fixture = TestBed.overrideComponent(MessageComponent, {
set: {
template: '<span>{{message}}</span>'
}})
fixture.detectChanges();
});
it('should set the message', async(inject([], () => {
fixture.componentInstance.setMessage('Test message');
fixture.detectChanges();
fixture.whenStable().then(() => {
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('span').innerText).toEqual('Test message');
});
})));
});