单元测试-收到TypeError:您提供了一个无效对象,该对象应在其中流

时间:2018-12-25 03:28:19

标签: angular jasmine ngrx jasmine-marbles

我正在尝试进行单元测试,其中效果使用服务从API提取结果,然后触发成功操作或错误操作。我不断收到错误TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.。不完全确定需要进行哪些更改才能使其正常工作。

包括错误检查测试和相关代码。

角度

Angular CLI: 7.1.4
Node: 10.5.0
OS: win32 x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.10.7
@angular-devkit/build-angular     0.10.7
@angular-devkit/build-optimizer   0.10.7
@angular-devkit/build-webpack     0.10.7
@angular-devkit/core              7.0.7
@angular-devkit/schematics        7.1.4
@angular/cdk                      7.2.0
@angular/material                 7.2.0
@ngtools/webpack                  7.0.7
@schematics/angular               7.1.4
@schematics/update                0.11.4
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.19.1

测试

describe('ResultsEffects', () => {
    let store: jasmine.SpyObj<Store<FeatureState>>;
    let ebs: jasmine.SpyObj<EBSService>;
    let snackbar: jasmine.SpyObj<SnackBarService>;

    beforeEach(() => {
        store = jasmine.createSpyObj('store', ['pipe']);
        ebs = jasmine.createSpyObj('EBSService', ['getPollResults', 'getUserHasVoted', 'getSettings']);
        snackbar = jasmine.createSpyObj('SnackBarService', ['notify']);
    });

    describe('fetchResults', () => {
        it(`should dispatch 'FetchPanelResultsError'`, () => {
            const fetchAction = new FetchPanelResults();
            const error = { statusText: 'ERROR' };
            const errorAction = new FetchPanelResultsError({ error: error.statusText });
            const values = {
                a: fetchAction,
                e: errorAction
            };
            const source = cold('a', values);
            const expected = cold('--e', values);
            const actions = new Actions(source);

            ebs.getPollResults.and.returnValue(throwError(error));

            const effects = new ResultsEffects(
                actions,
                store,
                snackbar,
                ebs
            );

            expect(
                effects.fetchResults$({
                    debounce: 20,
                    scheduler: getTestScheduler()
                })
            ).toBeObservable(expected);
        });
    });
});

效果

@Effect()
fetchResults$ = ({ debounce = 500, scheduler = asyncScheduler } = {}) => this.actions$.pipe(
    ofType<SetPanelResults>(PanelResultsActionTypes.FETCH_PANEL_RESULTS),
    // need alternate method for this
    // withLatestFrom(this.featureState$),
    // map(([_, state]) => state.twitch.token),
    debounceTime(debounce, scheduler),
    switchMap((token) =>
        this.ebs.getPollResults(token).pipe(
            map(res => new SetPanelResults(res.data.results)),
            catchError(error => of(new FetchPanelResultsError({ error: error.statusText })))
        )
    )
)

服务

getPollResults(token: string): Observable<PollResultsFetchData> {
    const options = {
        headers: ebsHeaders(token)
    };

    return this.http.get<PollResultsFetchData>(`${this.ebsURL}/poll/results`, options);
}

更新:添加了反跳以触发弹珠错误。仍然需要替代withLatestFrom

2 个答案:

答案 0 :(得分:1)

基于评论:

问题在于withLatest并没有真正返回任何内容,因为您没有要素状态。这是因为您使用jasmine.createSpyObj('store', ['pipe'])模拟了商店。

要解决此问题,请使用“真实的” ngrx存储或使用在NgRx 7中实现的mock store

答案 1 :(得分:0)

对我来说,在开发期间,Live Reload的错误开始定期出现几个月(Angular 7)。我只是通过重新打开浏览器并暂停了几秒钟来修复它。

今天重新打开并没有帮助,我偶尔发现,重新保存源文件之一(即使没有实际更改)也可以解决,并且错误消失了。重新保存可通过webpack和实时重新加载触发项目内存构建重新编译