我正在使用Karma 3。*,Jasmine,Angular 7和ngrx。
我正在尝试创建一个使用新MockStore的组件测试。
下面是代码示例:
describe('LiveRoomsComponent', () => {
let store: MockStore<StatsState>;
let component: LiveRoomsComponent;
let fixture: ComponentFixture<LiveRoomsComponent>;
beforeEach(async(() => {
const initialState = {
property1: '',
property2: 200,
property3: [],
property4: []
};
TestBed.configureTestingModule({
declarations: [LiveRoomsComponent],
imports: [ComponentsModule],
providers: [
provideMockStore({ initialState }),
{ provide: LinkResolverService, useClass: MockLinkResolverService }
]
}).compileComponents();
store = TestBed.get(Store);
}));
beforeEach(() => {
fixture = TestBed.createComponent(LiveRoomsComponent);
component = fixture.componentInstance;
});
describe('When WITH other LIVE ROOMS', () => {
beforeEach(() => {
store.setState({
property3: [{ displayName: 'BB' }, { displayName: 'CC' }],
property4: [{ displayName: 'AA'}],
property2: 10,
property1: 'ABC'
}); // set default state
});
it('should compile', () => {
expect(component).toBeTruthy();
});
it('should display LIVE ROOMS', () => {
console.log('HERE 4');
fixture.detectChanges();
// Nothing executes bellow this line
let liveRooms = fixture.debugElement.queryAll(By.css('.live-rooms'));
console.log('HERE 4.1');
expect(liveRooms.length).toBe(2);
});
[...]
在我的控制台中,我看到的是这里4,但是我从来没有看到这里的4.1。
关于正在发生的事情的任何想法。
顺便说一句。组件的changeDetection为默认值。