Created
April 8, 2016 17:00
-
-
Save makomweb/6a24f8ec64570159a6fd9918a010ae88 to your computer and use it in GitHub Desktop.
Observable null vs. empty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyTests | |
{ | |
public class Package { } | |
public class Service | |
{ | |
public IObservable<Package> DeliverRegular() | |
{ | |
return DeliverRegularAsync().ToObservable(); | |
} | |
private async Task<Package> DeliverNullAsync() | |
{ | |
return null; | |
} | |
public IObservable<Package> DeliverNull() | |
{ | |
return DeliverNullAsync().ToObservable(); | |
} | |
public IObservable<Package> DeliverEmpty() | |
{ | |
return Observable.Empty<Package>(); | |
} | |
private Task<Package> DeliverRegularAsync() | |
{ | |
return Task.FromResult(new Package()); | |
} | |
} | |
[Test] | |
public async Task When_regular_delivery_is_requested_it_should_not_be_null_or_empty() | |
{ | |
var service = new Service(); | |
var result = await service.DeliverRegular().FirstAsync(); | |
result.Should().NotBeNull(); | |
} | |
[Test] | |
public async Task When_null_delivery_is_requested_it_should_be_null() | |
{ | |
var service = new Service(); | |
var result = await service.DeliverNull().FirstAsync(); | |
result.Should().BeNull(); | |
} | |
[Test] | |
public async Task When_empty_delivery_is_requested_it_should_throw() | |
{ | |
var service = new Service(); | |
Assert.Throws<InvalidOperationException>(async () => { var result = await service.DeliverEmpty().FirstAsync(); }, "FirstASync() should complete but there is no item in the sequence!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment