ngrx效果单元测试失败
效果方法代码:
addNewLedgerAccountRequest$ = createEffect(() =>
this.actions$.pipe(
ofType(AddNewLedgerAccountActions.AddNewLedgerAccountActionTypes.ADD_NEW_LEDGER_ACCOUNT_REQUEST),
withLatestFrom(this.addNewLedgerAccountStore$.select(AddNewLedgerAccountSelectors.ledgerAccountFormSelector)),
switchMap(([, addNewLedgerAccountForm]: [Action, LedgerAccountFormDef]) => {
return this.addNewLedgerAccountService.addNewLedgerAccount$(addNewLedgerAccountForm).pipe(
map((ledgerAccount: LedgerAccountDef) => new AddNewLedgerAccountActions.AddNewLedgerAccountSuccess({ ledgerAccount })),
catchError(() => of(new AddNewLedgerAccountActions.AddNewLedgerAccountFailure()))
);
})
)
);
效果测试方法代码:
describe(`AddNewLedgerRequest$`, () => {
it(`should dispatch AddNewLedgerAccountRequest action if service succeeds`, () => {
const action = new fromActions.AddNewLedgerAccountRequest();
actions$ = hot('-a', { a: action });
const outcome = new fromActions.AddNewLedgerAccountSuccess({ ledgerAccount: mockedLedgerAccount });
const expected$ = cold('--b', { b: outcome });
const result$ = cold( '-b', { b: mockedLedgerAccount } );
addNewLedgerAccountSpy.mockReturnValue( result$ );
expect( effects.addNewLedgerAccountRequest$ ).toBeObservable( expected$ );
expect( addNewLedgerAccountSpy ).toHaveBeenCalledWith( mockedLedgerAccountForm );
} );
} );
结果:
Expected: --b,
Received: --?,
Expected:
[{"frame":20,"notification":{"kind":"N","value":{"payload":{"ledgerAccount":{"id":"1234","name":"TestLedger","number":"3344","type":"12","taxRate":"12","taxRatePercent":"12"}},"type":"[point - CONFIGURATION WIZARD] Add new ledger account - Success"},"hasValue":true}}]
Received:
[{"frame":20,"notification":{"kind":"N","value":{"payload":{"ledgerAccount":{"id":"1234","name":"TestLedger","number":"3344","type":"12","taxRate":"12","taxRatePercent":"12"}},"type":"[point - CONFIGURATION WIZARD] Add new ledger account - Success"}}}],
86 | const result$ = cold( '-b', { b: mockedLedgerAccount } );
87 | addNewLedgerAccountSpy.mockReturnValue( result$ );
> 88 | expect( effects.addNewLedgerAccountRequest$ ).toBeObservable( expected$ );
| ^
89 | expect( addNewLedgerAccountSpy ).toHaveBeenCalledWith( mockedLedgerAccountForm );
90 | } );
唯一的区别是Expected有,hasValue":true, Received没有。请让我知道如何解决它。谢谢你,
可能是package .json中jasmine-marbles的版本问题
我也面临着类似的问题。花了太多时间后,我没有找到任何解决办法。然后我从Package.json中删除了jasmine-marbles .
我重新安装了它:
npm install jasmine-marbles
在此之后,这些错误不再出现。
我建议你试一下。