我正在尝试测试一个简单的加载器组件,该组件正在从我制作的ngrx选择器获取其状态。选择器提供了一个对象(接口名称为LoaderState){queue: number}
,我将其映射为一个布尔值以显示或隐藏加载器。
为了对其进行测试,我使用了ngrx的茉莉花大理石和mockStore。我覆盖了选择器,使用期望值创建了一个预期的冷观测值,然后使用期望值进行观测。
我遵循了ngrx doc和Angular Up conf中的教程。
export class LoaderComponent implements OnInit {
state: Observable<boolean>
constructor(
private store: Store<State>
) { }
ngOnInit() {
this.state = this.store.select(selectLoader).pipe(
map(state => !!state.queue)
)
}
}
describe('LoaderComponent', () => {
let component: LoaderComponent
let fixture: ComponentFixture<LoaderComponent>
let store: MockStore<State>
let loaderSelector: MemoizedSelector<State, LoaderState>
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoaderComponent ],
providers: [ provideMockStore()]
}).compileComponents()
store = TestBed.get(Store)
loaderSelector = store.overrideSelector(selectLoader, {queue: 0})
}))
beforeEach(() => {
fixture = TestBed.createComponent(LoaderComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should hidden', () => {
const expected = cold('(a|)', { a: false })
expect(component.state).toBeObservable(expected)
})
})
我希望测试能够通过,并且代码也可以正常运行,但是测试失败,并且导致的错误是:
[ { frame: 0,
notification: { kind: 'N', value: false, error: undefined, hasValue: true } },
{ frame: 0,
notification: { kind: 'C', value: undefined, error: undefined, hasValue: false } } ]
Expected $.length = 1 to equal 2.
Expected $[1] = undefined to equal Object({ frame: 0, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }).