2017-08-24 158 views
1

我读过这个“大理石串”文档,但我坚持不明白。如何对rxjs5进行单元测试?

我想测试的基本上是一个“协议”。我有一个连接到Observable并发出数据的“东西”。在与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); 

在C#中它很容易观测连接到所有的事情,它也很容易等待。 IMO是一个非常易读的测试代码。

随着Rxjs5,我没有发现任何可能性等待或做任何类似的事情。所以,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实际上在BehaviourSubject呼吁next()

我很笨,有什么想法吗?

回答

0

有很多方法可以做到这一点,一个方法是使用超时如本例(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)) 
    }); 

我建议你看一看的RxJS reference,还有鲁特那里有好东西。根据您使用RxJS的方式和位置,您可能需要导入/加载某些内容才能访问某些功能,但应该在RxJS文档中对其进行相当清楚的解释。