首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试失败测试中的ngrx/效果命中成功操作

测试失败测试中的ngrx/效果命中成功操作
EN

Stack Overflow用户
提问于 2018-10-09 10:51:28
回答 1查看 230关注 0票数 0

我试着在我的效果中测试失败的动作,我模仿Brian Love's博客。然而,当我测试失败时,我收到一条错误消息,告诉我它期待一个成功的操作。

下面是我的代码:

search.service.ts:

代码语言:javascript
复制
    save(acronym: Acronym) {
    if (acronym.id) {
        return this.update(acronym);
    } else {
        return this.add(acronym);
    }
}

add(acronym: Acronym) {
    return this.db.collection(config.acronyms).add(acronym).then(() => this.search(acronym.code, acronym.project));
}

update(acronym: Acronym) {
    return this.db.collection(config.acronyms).doc(acronym.id).update(acronym).then(() => this.search(acronym.code, acronym.project));
}

effect.ts

代码语言:javascript
复制
    @Effect()
saveAcronym$ = this.actions$.pipe(
    ofType(acronymActions.SAVE_ACRONYM),
    map((action: acronymActions.SaveAcronym) => action.payload),
    mergeMap((payload) => {
        this._searchService.save(payload);
        return of(payload);
    }),
    map(data => new acronymActions.SaveAcronymSuccess(data)),
    catchError(error => of(new acronymActions.SaveAcronymFail(error)))
);

effect.spec.ts

代码语言:javascript
复制
        it("should return a SaveAcronymFail action, with an error, on fail", () => {
        const payload = {code: "SME", project: "SWAN"};
        const action = new fromActions.SaveAcronym(payload);
        const error = new Error();
        const result = new fromActions.SaveAcronymFail(error);

        actions = hot("-a", {a: action});
        const response = cold("-#|", {}, error);
        const expected = cold("--(b|)", {b: result});

        searchService.save = jest.fn(() => response);

        expect(effects.saveAcronym$).toBeObservable(expected);
    });

action.ts

代码语言:javascript
复制
export class SaveAcronymFail implements Action {
readonly type = SAVE_ACRONYM_FAIL;

constructor(public payload: Error) {}

}

reducer.ts:

代码语言:javascript
复制
        case AcronymActions.SAVE_ACRONYM_FAIL:
        return {...state, loading: false, loaded: true};

以及错误:

代码语言:javascript
复制
    expect(received).toEqual(expected)

Expected value to equal:
  [{"frame": 20, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value": {"payload": [Error], "type": "[ACRONYM] Save Fail"}}}, {"frame": 20, "notification": {"error": undefined, "hasValue": false, "kind": "C", "value": undefined}}]
Received:
  [{"frame": 10, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value": {"payload": {"code": "SME", "project": "SWAN"}, "type": "[ACRONYM] Save Success"}}}]

我试着修改框架,但没有成功。

EN

回答 1

Stack Overflow用户

发布于 2018-11-26 02:45:38

尝试此代码effect.spec.ts

代码语言:javascript
复制
it('should return an fail action, with an error, on failure', () => {
  const user = generateUser();
  const action = new AddData({ user });
  const error = new Error();
  const outcome = new AddDatafail({ error });
  actions = hot('-a|', { a: action });
  const response = cold('-#|', {}, error);
  const expected = cold('--(b|)', { b: outcome });
  serService.saveSampleData = jest.fn(() => response);
  expect(effects.AddData).toBeObservable(expected););
});
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52712677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档