简体   繁体   English

Observable的简单测试结果是带有chai和mocha的nodejs

[英]Simple tests of Observable results in nodejs with chai and mocha

I am developing an app using Nodejs, RxJS and Typescript. 我正在使用Nodejs,RxJS和Typescript开发一个应用程序。

The app has a function which returns an Observable of a string 该应用程序有一个函数,它返回一个字符串的Observable

myObsFunction() : Observable<string> {
... do stuff
}

I would like to be able to make a simple test to check that when I subscribe to this function I get the expected string. 我希望能够做一个简单的测试来检查当我订阅这个函数时,我得到了预期的字符串。 I am using chai and mocha and so I write the following test case 我正在使用chaimocha ,因此我编写了以下测试用例

import { expect } from 'chai';
import 'mocha';

import {myObsFunction} from './my-source-file';

describe('myObsFunction function', () => {

    it('check myObsFunction', () => {
        const expectedString = 'abc';
        let receivedString: string;
        myObsFunction().subscribe(
            data => receivedString = data,
            error => console.error(error),
            () => expect(receivedString).to.equal(expectedString)
        )
    });

});

Unfortunately this test case does not work as expected by me. 不幸的是,这个测试用例并不像我预期的那样有效。 It always behaves as it has been successfully passed even in case of errors. 它始终表现为即使出现错误也已成功通过。 The expect check which I have written in the onCompleted function does not signal anything even when the expectedString is not equal the the receivedString. 即使onCompleted不等于receivedString,我在onCompleted函数中写的expect检查也没有发出任何信号。 The onCompleted function is actually executed (I can see this just adding a console.log instruction in the onCompleted function) but the expect does not signal any error when there are errors onCompleted函数实际上是执行的(我可以看到这只是在onCompleted函数中添加了一个console.log指令)但是当出现错误时,expect不会发出任何错误信号

Is there any way to run such simple tests without having to start using Schedulers and more complex mechanisms? 有没有办法运行这样简单的测试而无需开始使用调度程序和更复杂的机制?

The test logic looks sound, here's a working example with mocha and chai. 测试逻辑看起来很合理,这是mocha和chai的一个工作示例。

 console.clear() const Observable = Rx.Observable mocha.setup('bdd'); const assert = chai.assert; const should = chai.should(); const expect = chai.expect; const done = mocha.done; const myObsFunction = () => Observable.of('xyz'); const myAsyncObsFunction = () => Observable.timer(500).mapTo('xyz'); describe('RxJs Observable Test Examples', function() { it('should test the observable succeeds', function () { const expectedString = 'xyz'; let receivedString: string; myObsFunction().subscribe( data => receivedString = data, error => console.error(error), () => { expect(receivedString).to.equal(expectedString); } ) }); it('should test the observable fails', function () { const expectedString = 'abc'; let receivedString: string; myObsFunction().subscribe( data => receivedString = data, error => console.error(error), () => { expect(receivedString).to.equal(expectedString); } ) }); it('should test the async observable succeeds', function (done) { const expectedString = 'xyz'; let receivedString: string; myAsyncObsFunction().subscribe( data => receivedString = data, error => console.error(error), () => { //expect(receivedString).to.equal(expectedString); if (receivedString !== expectedString) { return done(new Error("Failed match")); } else { return done(); } } ) }); it('should test the async observable fails', function (done) { const expectedString = 'abc'; let receivedString: string; myAsyncObsFunction().subscribe( data => receivedString = data, error => console.error(error), () => { //expect(receivedString).to.equal(expectedString); if (receivedString !== expectedString) { return done(new Error("Failed match")); } else { return done(); } } ) }); }); mocha.run(); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.js"></script> <div id="mocha"></div> 


False positives when the observable never fires 当观察者从未发射时出现误报

One thing I came across was false positives if the observable never fires. 我遇到的一件事是假阳性,如果观察者永远不会发射。 Here's some helper functions I use to overcome that problem. 这是我用来克服这个问题的一些辅助函数。 Note that take(1) ensures the completed event is fired, even if the observable does not itself complete. 请注意, take(1)确保触发已完成的事件,即使observable本身不完整也是如此。

 console.clear() const Observable = Rx.Observable mocha.setup('bdd'); const assert = chai.assert; const should = chai.should(); const expect = chai.expect; const subscribeAndTestValue = function (observable: Observable<any>, expected: any): string { let fail = ''; let wasSubscribed = false; const sub = observable .take(1) .subscribe( (result) => { if (result !== expected) { fail = 'Subscription result does not match expected value'; } wasSubscribed = true; }, (error) => { fail = 'Subscription raised an error'; }, (/*completed*/) => { // When testing a single value, // need to check that the subscription was activated, // otherwise the expected value is never tested if (!wasSubscribed) { fail = 'Subscription produced no results'; } } ); sub.unsubscribe(); return fail; } const subscribeAndTestNoDataEmitted = function (observable: Observable<any>): string { let fail; let wasSubscribed = false; const sub = observable .subscribe( (result) => { wasSubscribed = true; }, (error) => { fail = 'Subscription raised an error'; }, (/*completed*/) => { if (wasSubscribed) { fail = 'Subscription produced values when none were expected'; } } ); sub.unsubscribe(); return fail; } const emptyObservable = Observable.empty(); const nonCompletingObservable = Observable.interval(1000); const emittingObservable = Observable.of('abc'); describe('RxJs Observable Test Examples', function() { it('should test the observable fires', function () { const expectedString = 'xyz'; const failed = subscribeAndTestValue(emptyObservable, expectedString); expect(failed).to.equal('Subscription produced no results'); }); it('should test first observable value of a non-completing observable', function () { const expectedString = '0'; const failed = subscribeAndTestValue(nonCompletingObservable, expectedString); expect(failed).to.equal(''); }); it('should test the observable does not fire', function () { const expectedString = 'xyz'; const failed = subscribeAndTestNoDataEmitted(emittingObservable, expectedString); expect(failed).to.equal('Subscription produced values when none were expected'); }); }); mocha.run(); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.js"></script> <div id="mocha"></div> 

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

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