【问题标题】:Type 'TestHotObservable' is missing the following properties from type 'Subject<any>': observers, closed, isStopped, hasError, and 5 more类型“TestHotObservable”缺少类型“Subject<any>”的以下属性:observers、closed、isStopped、hasError 和另外 5 个
【发布时间】:2019-03-01 12:26:39
【问题描述】:

我正在尝试对效果进行简单的单元测试。我正在尝试实现此示例中的代码: https://github.com/ngrx/platform/blob/master/docs/effects/testing.md 但不幸的是,由于 authActions,我无法编译代码。这一行:

authActions = hot('--a-', { a: action });

给我编译错误,例如:

类型“TestHotObservable”缺少类型中的以下属性 'Subject':观察者、关闭、isStopped、hasError 等 5 个。

这里是代码 sn-p:

import { AuthEffects } from "./auth.effects";
import { Subject } from 'rxjs';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import * as AuthActions from './auth.actions';
import { hot, cold } from 'jasmine-marbles';
import { RouterTestingModule } from '@angular/router/testing';

describe('AuthEffects', () => {
    let authEffects: AuthEffects;
    let authActions: Subject<any>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                RouterTestingModule
            ],
            providers: [
                AuthEffects,
                provideMockActions(() => authActions)
            ]
        });

        authEffects = TestBed.get(AuthEffects);
    });

    it('effect test', () => {
        let username = '';
        let password = '';
        let role = 'PARENT';
        const action = new AuthActions.TrySignin({ username, password, role });
        const completion = new AuthActions.SigninUser()

        authActions = hot('--a-', { a: action });
        const expected = cold('--b', { b: completion });

        expect(authEffects.authSignin).toBeObservable(expected);
    })
})

由于我是新手,所以我没有想法。这里可能有什么问题?

【问题讨论】:

    标签: angular unit-testing karma-jasmine ngrx


    【解决方案1】:

    您必须将操作定义为 Observable - https://ngrx.io/guide/effects/testing

    let actions: Observable<any>;
    
    ...
    
     it('should work', () => {
        actions = hot('--a-', { a: action });;
      });
    
    

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2021-10-02
      • 2021-02-04
      • 2020-01-30
      • 1970-01-01
      • 2020-01-03
      相关资源
      最近更新 更多