简体   繁体   English

测试Angular observable是否发出值或序列

[英]Test that an Angular observable emits a value or sequence

I have a service that creates an observable that emits values, and it's relatively easy to test that an emitted value is as expected. 我有一个服务创建一个可以发出值的observable,并且测试一个发出的值是否符合预期相对容易。

For example: 例如:

describe('service', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({providers: [MyService]});
    });

    it('should emit true', async(() => {
        const service = TestBed.get(MyService);
        service.values$.subscribe((value) => expect(value).toBeTruthy());
    }));
});

The above works to test the expected value, but it only works if the service actually emits a value. 以上工作用于测试预期值,但仅在服务实际发出值时才有效。 If you have the edge case where the service fails to emit a value, then the test itself actually passes and Jasmine logs the warning message "SPEC HAS NO EXPECTATIONS should be created". 如果你有服务无法发出值的边缘情况,那么测试本身实际上会通过,并且Jasmine会记录警告消息“SPEC HAS NO EXPECTATIONS should be created”。

I searched Google for a while trying to figure out how to catch this case as an error and came up with this approach. 我搜索谷歌一段时间试图弄清楚如何将这个案例作为一个错误抓住并提出了这种方法。

    it('should emit true', async(() => {
        const service = TestBed.get(MyService);
        let value;
        service.values$.subscribe((v) => value = v);
        expect(value).toBeTruthy();
    }));

The above works only for synchronous observables and feels like code smell to me. 以上只适用于同步观察,并感觉像代码闻到我。 Another developer will see this and think it's a poor quality test. 另一位开发人员会看到这一点,并认为这是一个质量差的测试。

So after thinking about this for a few days. 经过几天考虑之后。 I thought of using takeUntil() to force the observable to complete and then test the expected result then. 我想使用takeUntil()强制observable完成然后测试预期的结果。

For example: 例如:

describe('service', () => {
    let finished: Subject<void>;

    beforeEach(() => {
        TestBed.configureTestingModule({providers: [MyService]});
        finished = new Subject();
    });

    afterEach(() => {
        finished.next();
        finished.complete();
    });

    it('should emit true', async(() => {
        const service = TestBed.get(MyService);
        let value;
        service.changes$
            .pipe(
                takeUntil(finished),
                finalize(() => expect(value).toBeTruthy())
            )
            .subscribe((v) => value = v);
    }));
});

In the above example the value is being stored in a local variable and then the expected result is checked when the observable completes. 在上面的示例中,值存储在局部变量中,然后在observable完成时检查预期结果。 I force the completion by using afterEach() with takeUntil() . 我使用带有takeUntil() afterEach()强制完成。

Question: 题:

Are there any side effects with my approach, and if so what would be the more Angular/Jasmine way of performing these kinds of tests. 我的方法是否有任何副作用,如果是这样,那么Angular / Jasmine执行这些测试的方式会更多。 I am worried that you are not suppose to perform expect assertions during the afterEach() life-cycle call. 我担心你不想在afterEach()生命周期调用期间执行expect断言。

This seems overkill to me. 这对我来说似乎有些过分。

Jasmine offers a callback in its tests, you could simply use it ? Jasmine在测试中提供回调,您可以简单地使用它吗?

it('should X', doneCallback => {
  myObs.subscribe(res => {
    expect(x).toBe(y);
    doneCallback();
  });
});

If the callback isn't called, the test fails with a timeout exception (meaning no more test will run after this failed one) 如果未调用回调,则测试将失败并显示超时异常(意味着在此失败后将不再运行测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM