0

我想测试一个 Angular 服务,我将简化我的案例以使其简单:

我有一个behaviourSubject 绑定到我的服务中的一个可观察对象

@Injectable({
  providedIn: 'root',
})
export class MyService {
    readonly username = new BehaviorSubject<string>('');
    readonly username$ = this.username.asObservable();

    constructor() {}
}

我要测试的是多次填写用户名的简单用户交互:

describe('MyService', () => {
    let myService: MyService;

    const testScheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
    });

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [{ provide: MyService, useValue: myService }]
        });

        httpTestingController = TestBed.inject(HttpTestingController);
    });

    afterEach(() => {
        // After every test, assert that there are no more pending requests.
        httpTestingController.verify();
    });

    it('should be created', inject([MyService], (service: MyService) => {
        expect(service).toBeTruthy();
    }));

    it('should be toto lala', inject([MyService], (service: MyService) => {
        testScheduler.run(helpers => {
            const { expectObservable, cold } = helpers;
            service.username.next("toto");
            service.username.next("lala");
            const expect$ = "ab";
            expectObservable(service.username$).toBe(expect$, {
                a: "toto",
                b: "lala",
            })
        });
    }));

但是当我启动这个测试时,只有主体发出的最后一个值在 observable 中。

Chrome 97.0.4692.99 (Windows 10) MyService should be toto lala FAILED
        Expected $.length = 1 to equal 2.
        Expected $[0].notification.value = 'lala' to equal 'toto'.
        Expected $[1] = undefined to equal Object({ frame: 1, notification: Notification({ kind: 'N', value: 'lala', error: undefined, hasValue: true }) }).
        Error: Expected $.length = 1 to equal 2.
        Expected $[0].notification.value = 'lala' to equal 'toto'.
        Expected $[1] = undefined to equal Object({ frame: 1, notification: Notification({ kind: 'N', value: 'lala', error: undefined, hasValue: true }) }).
            at <Jasmine>
            at TestScheduler.assertDeepEqual (mycomponent.component.spec.ts:71:24)
            at node_modules/rxjs/_esm2015/internal/testing/TestScheduler.js:110:1
            at <Jasmine>
Chrome 97.0.4692.99 (Windows 10): Executed 1 of 7 (1 FAILED) (0 secs / 0.024 secs)
Chrome 97.0.4692.99 (Windows 10) MyService should be toto lala FAILED
        Expected $.length = 1 to equal 2.
        Expected $[0].notification.value = 'lala' to equal 'toto'.
        Expected $[1] = undefined to equal Object({ frame: 1, notification: Notification({ kind: 'N', value: 'lala', error: undefined, hasValue: true }) }).
        Error: Expected $.length = 1 to equal 2.
        Expected $[0].notification.value = 'lala' to equal 'toto'.
        Expected $[1] = undefined to equal Object({ frame: 1, notification: Notification({ kind: 'N', value: 'lala', error: undefined, hasValue: true }) }).
            at <Jasmine>
            at TestScheduler.assertDeepEqual (mycomponent.component.spec.ts:71:24)
            at node_modules/rxjs/_esm2015/internal/testing/TestScheduler.js:110:1
Chrome 97.0.4692.99 (Windows 10): Executed 2 of 7 (1 FAILED) (skipped 5) (0.126 secs / 0.029 secs)
TOTAL: 1 FAILED, 1 SUCCESS
TOTAL: 1 FAILED, 1 SUCCESS
npm ERR! Test failed.  See above for more details.

有没有办法在不将 BehaviorSubject 更改为 ReplaySubject 的情况下在 observable 中获取 2 个值?

4

1 回答 1

0

不,这是不可能的,behaviourSubject只能缓存最后一个值。如果您只是想测试订阅和冗余,我下次见两次,为什么不下一次并期待它之后但做两次。

于 2022-02-01T10:44:50.687 回答