[英]How to unit-test rxjs5?
I've read through this "marble-string" docs, but I juts don't get it. 我已经阅读了此“大理石字符串”文档,但我认为没有。
What I want to test is basically a "protocol". 我要测试的基本上是一个“协议”。 I have a "thing" which is connected to an Observable and which emits data.
我有一个连接到Observable并发出数据的“东西”。 In my backend code with Rx.NET and C# I have written a helper to test protocols so that my tests look like this:
在使用Rx.NET和C#的后端代码中,我编写了一个帮助程序来测试协议,以便我的测试如下所示:
verifier.Send(new AddMaterialPickCommand(commandId, orderId, materialId: Guid.NewGuid(), quantity: 1))
.FailWhen<CommandFailedEvent>(e => e.CommandId == commandId)
.ThenExpect<MaterialPickAddedEvent>(e => e.EntityId == orderId)
.ThenSend(new DeleteOrderCommand(commandId, orderId))
.FailWhen<CommandFailedEvent>(e => e.CommandId == commandId)
.ThenExpect<OrderDeletedEvent>(e => e.EntityId == orderId)
.ExecuteAndWait(Timeout);
In C# its very easy to connect observables to all things and its also very easy to wait. 在C#中,它非常容易将可观察对象连接到所有事物,并且也非常容易等待。 IMO it is a very readable test code.
IMO这是一个非常易读的测试代码。
With Rxjs5, I didn't find any possibility to wait or do anything similar. 使用Rxjs5,我没有发现等待或执行任何类似操作的可能性。 So atm I'm failing at just checking that a single thing emitted by my observable is the one expected:
所以atm我无法检查一下我的可观察到的东西是否是预期的东西:
sut.setValue("key1", "key2");
sut.getObservable("key1", "key2")
.subscribe(value => {
expect(value).toBe("testValue") // I want either execute this or fail after a timeout
});
setValue actually calls next()
on a BehaviourSubject. setValue实际上在BehaviourSubject上调用
next()
。
I'm clueless, any ideas? 我一无所知,有什么想法吗?
There are many ways to do this, one way is with a timeout as demonstrated in this example ( working example ): 有许多方法可以做到这一点,其中一种方法是如本示例( 工作示例 )中所示的超时:
// Generates a value every second starting one second after subscription
const eventEmulatorStream = Rx.Observable.interval(1000);
// Listen to the event stream and timeout after 500 ms.
// On timeout an error will be generated that will be caught in the
// 'error' part of the subscribe.
eventEmulatorStream
.timeout(500)
.subscribe({
next: value => console.log(value),
error: err => console.log(JSON.stringify(err))
});
I suggest that you take a look at the RxJS reference , there are loots of goodies in there. 我建议您看一下RxJS 参考 ,那里有很多东西。 Depending on how and where you are using RxJS you might need to import/load some things to get access to some of the features, but it should all be explained quite clearly in the RxJS documentation.
根据使用RxJS的方式和位置,可能需要导入/加载一些东西才能访问某些功能,但是所有这些都应该在RxJS文档中清楚地解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.