关于ZAKER 融媒体解决方案 合作 加入

node.js- 使用 chai 和 mocha 在 nodejs 中对可观察结果 .

CocoaChina 11-11

我正在使用 Nodejs,RxJS 和 Typescript 开发应用程序 .

该应用程序具有返回字符串的 Observable 的功能

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

我希望能够进行一个简单的测试 , 以检查当我订阅此函数时是否得到了预期的字符串 . 我正在使用 chai 和 mocha, 所以我写了以下测试用例

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 ) ) } ) ;} ) ;

不幸的是 , 这个测试用例不能按我的预期工作 . 即使出现错误 , 它也始终会像成功通过一样运行 . 我在 onCompleted 函数中编写的期望检查不会发出任何信号 , 即使 ExpectedString 不等于 ReceivedString 也是如此 . onCompleted 函数实际上已执行 ( 我可以在 onCompleted 函数中添加 console.log 指令来看到它 ) , 但是期望在发生错误时不会发出任何错误信号

有什么方法可以运行这种简单的测试而不必开始使用 Scheduler 和更复杂的机制?

最佳答案

测试逻辑看起来不错 , 这是有关 mocha 和 chai 的有效示例 .

console.clear ( ) const Observable = Rx.Observablemocha.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>

当可观察者永不触发时的误报

我碰到的一件事是 , 如果可观察到的信号永远不会触发 , 那么就会出现误报 . 这是我用来克服该问题的一些辅助函数 . 请注意 , 即使可观察对象本身未完成 ,take ( 1 ) 也会确保触发了 complete 事件 .

console.clear ( ) const Observable = Rx.Observablemocha.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>

以上内容由"CocoaChina"上传发布 查看原文
相关标签 结果观察者

觉得文章不错,微信扫描分享好友

扫码分享