Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyCoolViewModel: ReactiveObject
- {
- // Create a Property to store the results
- ObservableAsPropertyHelper<byte[]> _BytesWeHaveRead;
- public byte[] BytesWeHaveRead {
- get { return _BytesWeHaveRead.Value; }
- }
- // The command we'll be testing
- ReactiveAsyncCommand ReadBytesCommand { get; private set; }
- public MyCoolViewModel(Func<IObservable<byte[]>> readBytesFunc)
- {
- ReadBytesCommand = new ReactiveAsyncCommand();
- // Take our Command, send it through the readBytesFunc function, then
- // pipe the results to the BytesWeHaveRead property
- _BytesWeHaveRead = ReadBytesCommand
- .RegisterAsyncObservable(_ => readBytesFunc())
- .ToProperty(this, x => x.BytesWeHaveRead);
- }
- }
- IObservable<byte[]> mockReadBytesAsync()
- {
- // Wait ten seconds, then return the byte array
- return Observable.Return(new byte[] {1,2,3}).Delay(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler);
- }
- [Fact]
- public void ReadBytesAsyncCommandTest()
- {
- // Replace all schedulers with the TestScheduler
- (new TestScheduler()).With(sched => {
- var fixture = new MyCoolViewModel(mockReadBytesAsync);
- // Execute the command - remember that it should take 10 seconds to
- // execute before returning
- fixture.ReadBytesCommand.Execute(null);
- // 1 second in, it should still be running, the results should be empty
- sched.RunToMilliseconds(1000)
- Assert.False(fixture.ReadBytesCommand.CanExecute(null));
- Assert.Null(fixture.BytesWeHaveRead);
- // 9 seconds in, same deal. Remember though, this doesn't take 9 seconds
- // of actual wall time to execute, this entire test is finished instantly
- sched.RunToMilliseconds(9000)
- Assert.False(fixture.ReadBytesCommand.CanExecute(null));
- Assert.Null(fixture.BytesWeHaveRead);
- // 11 seconds in, it should be complete - we should be able to read
- // another block since we're done with the first one.
- sched.RunToMilliseconds(11000)
- Assert.True(fixture.ReadBytesCommand.CanExecute(null));
- Assert.Equal(1, fixture.BytesWeHaveRead[0]);
- Assert.Equal(2, fixture.BytesWeHaveRead[1]);
- Assert.Equal(3, fixture.BytesWeHaveRead[2]);
- });
- }
Add Comment
Please, Sign In to add comment