我试图使用Microsoft.Reactive.Testing.ReactiveTest
来测试应该抛出超时异常的observable。
我无法找到让超时出现的方法。测试始终以空res.Messages
以下是测试中的代码:
public class DeviceHandler : IDeviceHandler
{
public readonly TimeSpan DISCOVER_SERVICE_TIMEOUT = TimeSpan.FromSeconds(10);
public IObservable<IGattService> DiscoverService(IDevice device)
{
return device
.GetKnownService(SERVICE_UUID)
.Timeout(DISCOVER_SERVICE_TIMEOUT);
}
}
这是测试代码:
public class DeviceHandlerTest : ReactiveTest
{
private DeviceHandler deviceHandler;
private TestScheduler scheduler;
[TestInitialize]
public void SetUp()
{
deviceHandler = new DeviceHandler();
scheduler = new TestScheduler();
}
[TestMethod]
public void ShouldErrorIfNoServiceFound()
{
// GIVEN
var mockDevice = new Mock<IDevice>();
var mockService = new Mock<IGattService>();
var mockServiceObservable = scheduler.CreateColdObservable(new Recorded<Notification<IGattService>>[]{
});
mockDevice.Setup(device => device.GetKnownService(deviceHandler.SERVICE_UUID))
.Returns(mockServiceObservable);
// WHEN
var res = scheduler.Start(() => deviceHandler.DiscoverService(mockDevice.Object));
scheduler.Sleep(20000); // I expect this to enable the timeout to trigger
// THEN
Assert.IsTrue(Enumerable.SequenceEqual(res.Messages, new Recorded<Notification<IGattService>>[] {
OnError<IGattService>(100, new TimeoutException())
}, new RecordedComparer<IGattService>()));
}
当我拨打scheduler.Sleep(20000)
(20秒,超过10秒超时)时,我希望触发超时。
我做错了什么?
由于