Beispiel #1
0
        public void Execute_WithObservableYielding3Values_ShouldReturnCorrectResult(
            object value1,
            object value2,
            object value3,
            TestSchedulers schedulers,
            ThrowingTestScheduler scheduler)
        {
            //arrange

            var observable = scheduler.CreateColdObservable(OnNext(10, value1),
                                                            OnNext(20, value2),
                                                            OnNext(30, value3),
                                                            OnCompleted(40, new object()));
            var observer = scheduler.CreateObserver <object>();
            var sut      = new ObservableMvvmCommand <object, object>(o => observable, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy <object>(), doObserver: () => observer, doScheduler: ImmediateScheduler.Instance);

            //act
            sut.Execute(null);
            //allow any work scheduled with the schedulers to advance
            schedulers.AdvanceBy(200);
            //allow notifications to advance
            scheduler.AdvanceBy(200);

            //assert
            observer.Messages.AssertEqual(OnNext(10, value1),
                                          OnNext(20, value2),
                                          OnNext(30, value3),
                                          OnCompleted(40, new object()));
        }
Beispiel #2
0
        public void Execute_WhenExuctingWhileOtherExecutionIsOccuring_ShouldCancelPreviousExecution(
            [Frozen] TestSchedulers schedulers)
        {
            //arrange
            var firstObservable  = schedulers.CreateColdObservable(OnNext(200, new object()), OnNext(300, new object()));
            var secondObservable = schedulers.CreateColdObservable(OnNext(400, new object()), OnNext(500, new object()));
            var i = 0;
            Func <object, IObservable <object> > observableFactory = _ => ++ i == 1 ? firstObservable : secondObservable;
            var sut = new ObservableMvvmCommand <object, object>(observableFactory, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy <object>());

            //act
            sut.Execute(null);
            schedulers.AdvanceBy(250);
            sut.Execute(null);
            schedulers.AdvanceBy(350);

            //assert
            firstObservable.Subscriptions.Single().Unsubscribe.Should().Be(250);
        }
Beispiel #3
0
        public void Execute_ShouldCallNotifyExecuting(
            [Frozen] Mock <ICanExecuteStrategy <object> > canExecute,
            ObservableMvvmCommand <object, object> sut,
            [Frozen] object obj,
            TestSchedulers schedulers)
        {
            //arrange

            //act
            sut.Execute(obj);
            schedulers.AdvanceBy(200);

            //assert
            canExecute.Verify(c => c.NotifyExecuting(obj), Times.Once());
        }
Beispiel #4
0
        public void Execute_WhenComplete_ShouldCallNotifyNotExecuting(
            Mock <ICanExecuteStrategy <object> > canExecute,
            object obj,
            TestSchedulers schedulers)
        {
            //arrange
            var value = Observable.Return(obj, ImmediateScheduler.Instance);
            var sut   = new ObservableMvvmCommand <object, object>(_ => value, schedulers, schedulers, "name", canExecute.Object);

            //act
            sut.Execute(obj);
            schedulers.AdvanceBy(200);

            //assert
            canExecute.Verify(c => c.NotifyNotExecuting(obj), Times.Once());
        }
Beispiel #5
0
        public void Execute_ShouldExecuteObservable(
            TestSchedulers schedulers)
        {
            //arrange
            bool observableCalled = false;
            var  value            = Observable.Return(Unit.Default, ImmediateScheduler.Instance)
                                    .Do(unit => observableCalled = true);
            var sut = new ObservableMvvmCommand <object, System.Reactive.Unit>(o => value, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy <object>());

            //act
            sut.Execute(null);
            schedulers.AdvanceBy(200);

            //assert
            observableCalled.Should().BeTrue();
        }
Beispiel #6
0
        public void Execute_WithError_ShouldExecuteErrorTask(
            object obj,
            Exception expected,
            TestSchedulers schedulers)
        {
            //arrange
            Exception actual = null;
            var       value  = Observable.Throw <object>(expected, ImmediateScheduler.Instance);
            Func <CancellationToken, Exception, Task> errorTask = async(ct, ex) => actual = ex;
            var sut = new ObservableMvvmCommand <object, object>(_ => value, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy <object>(), errorTask: errorTask);

            //act
            sut.Execute(obj);
            schedulers.AdvanceBy(200);

            //assert
            actual.Should().Be(expected);
        }
Beispiel #7
0
        public void Execute_WhenErrorInErrorAction_ShouldCallNotifyNotExecuting(
            Mock <ICanExecuteStrategy <object> > canExecute,
            Exception error,
            object obj,
            TestSchedulers schedulers)
        {
            //arrange
            var value = Observable.Throw <object>(error, ImmediateScheduler.Instance);
            Func <CancellationToken, Exception, Task> errorTask = async(ct, ex) =>
            {
                throw new Exception();
            };
            var sut = new ObservableMvvmCommand <object, object>(_ => value, schedulers, schedulers, "name", canExecute.Object, errorTask: errorTask);

            //act
            sut.Execute(obj);
            schedulers.AdvanceBy(200);

            //assert
            canExecute.Verify(c => c.NotifyNotExecuting(obj), Times.Once());
        }
Beispiel #8
0
        public void Execute_ShouldCallOnError(
            Exception expected,
            TestSchedulers schedulers)
        {
            //arrange
            Exception actual = null;
            var       value  = Observable.Throw <object>(expected, ImmediateScheduler.Instance);
            var       sut    = new ObservableMvvmCommand <object, object>(
                o => value,
                schedulers,
                schedulers,
                "name",
                new AlwaysTrueCanExecuteStrategy <object>(),
                doObserver: () => Observer.Create <object>(_ => { }, e => actual = e),
                doScheduler: ImmediateScheduler.Instance);

            //act
            sut.Execute(null);
            schedulers.AdvanceBy(200);

            //assert
            actual.Should().Be(expected);
        }
Beispiel #9
0
        public void Execute_ShouldCallCallback(
            object expected,
            TestSchedulers schedulers)
        {
            //arrange
            object actual = null;
            var    value  = Observable.Return(expected, ImmediateScheduler.Instance);
            var    sut    = new ObservableMvvmCommand <object, object>(
                o => value,
                schedulers,
                schedulers,
                "name",
                new AlwaysTrueCanExecuteStrategy <object>(),
                doObserver: () => Observer.Create <object>(o => actual = o),
                doScheduler: ImmediateScheduler.Instance);

            //act
            sut.Execute(null);
            schedulers.AdvanceBy(200);

            //assert
            actual.Should().Be(expected);
        }
Beispiel #10
0
        public void Execute_ShouldBeCalledWithArg(
            IObservable <object> value,
            object arg,
            TestSchedulers schedulers)
        {
            //arrange
            object actual = null;
            var    sut    = new ObservableMvvmCommand <object, object>(o =>
            {
                actual = o;
                return(value);
            },
                                                                       schedulers,
                                                                       schedulers,
                                                                       "name",
                                                                       new AlwaysTrueCanExecuteStrategy <object>());

            //act
            sut.Execute(arg);
            schedulers.AdvanceBy(200);

            //assert
            actual.Should().Be(arg);
        }
		public void Execute_ShouldExecuteObservable(
			TestSchedulers schedulers)
		{
			//arrange
			bool observableCalled = false;
			var value = Observable.Return(Unit.Default, ImmediateScheduler.Instance)
				.Do(unit => observableCalled = true);
			var sut = new ObservableMvvmCommand<object, System.Reactive.Unit>(o => value, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy<object>());

			//act
			sut.Execute(null);
			schedulers.AdvanceBy(200);

			//assert
			observableCalled.Should().BeTrue();
		}
		public void Execute_ShouldCallNotifyExecuting(
			[Frozen]Mock<ICanExecuteStrategy<object>> canExecute,
			ObservableMvvmCommand<object, object> sut,
			[Frozen]object obj,
			TestSchedulers schedulers)
		{
			//arrange

			//act
			sut.Execute(obj);
			schedulers.AdvanceBy(200);

			//assert
			canExecute.Verify(c => c.NotifyExecuting(obj), Times.Once());
		}
		public void Execute_ShouldCallOnError(
			Exception expected,
			TestSchedulers schedulers)
		{
			//arrange
			Exception actual = null;
			var value = Observable.Throw<object>(expected, ImmediateScheduler.Instance);
			var sut = new ObservableMvvmCommand<object, object>(
				o => value,
				schedulers,
				schedulers,
				"name",
				new AlwaysTrueCanExecuteStrategy<object>(),
				doObserver: () => Observer.Create<object>(_ => { }, e => actual = e),
				doScheduler: ImmediateScheduler.Instance);

			//act
			sut.Execute(null);
			schedulers.AdvanceBy(200);

			//assert
			actual.Should().Be(expected);
		}
		public void Execute_WhenErrorInErrorAction_ShouldCallNotifyNotExecuting(
			Mock<ICanExecuteStrategy<object>> canExecute,
			Exception error,
			object obj,
			TestSchedulers schedulers)
		{
			//arrange
			var value = Observable.Throw<object>(error, ImmediateScheduler.Instance);
			Func<CancellationToken, Exception, Task> errorTask = async (ct, ex) =>
			{
				throw new Exception();
			};
			var sut = new ObservableMvvmCommand<object, object>(_ => value, schedulers, schedulers, "name", canExecute.Object, errorTask: errorTask);

			//act
			sut.Execute(obj);
			schedulers.AdvanceBy(200);

			//assert
			canExecute.Verify(c => c.NotifyNotExecuting(obj), Times.Once());
		}
		public void Execute_WhenError_ShouldCallNotifyNotExecuting(
			Mock<ICanExecuteStrategy<object>> canExecute,
			Exception error,
			object obj,
			TestSchedulers schedulers)
		{
			//arrange
			var value = Observable.Throw<object>(error, ImmediateScheduler.Instance);
			var sut = new ObservableMvvmCommand<object, object>(_ => value, schedulers, schedulers, "name", canExecute.Object);

			//act
			sut.Execute(obj);
			schedulers.AdvanceBy(200);

			//assert
			canExecute.Verify(c => c.NotifyNotExecuting(obj), Times.Once());
		}
		public void Execute_WithObservableYielding3Values_ShouldReturnCorrectResult(
			object value1,
			object value2,
			object value3,
			TestSchedulers schedulers,
			ThrowingTestScheduler scheduler)
		{
			//arrange

			var observable = scheduler.CreateColdObservable(OnNext(10, value1),
				OnNext(20, value2),
				OnNext(30, value3),
				OnCompleted(40, new object()));
			var observer = scheduler.CreateObserver<object>();
			var sut = new ObservableMvvmCommand<object, object>(o => observable, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy<object>(), doObserver: () => observer, doScheduler: ImmediateScheduler.Instance);

			//act
			sut.Execute(null);
			//allow any work scheduled with the schedulers to advance
			schedulers.AdvanceBy(200);
			//allow notifications to advance
			scheduler.AdvanceBy(200);

			//assert
			observer.Messages.AssertEqual(OnNext(10, value1),
				OnNext(20, value2),
				OnNext(30, value3),
				OnCompleted(40, new object()));
		}
		public void Execute_WithError_ShouldExecuteErrorTask(
		  object obj,
			Exception expected,
			TestSchedulers schedulers)
		{
			//arrange
			Exception actual = null;
			var value = Observable.Throw<object>(expected, ImmediateScheduler.Instance);
			Func<CancellationToken, Exception, Task> errorTask = async (ct, ex) => actual = ex;
			var sut = new ObservableMvvmCommand<object, object>(_ => value, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy<object>(), errorTask: errorTask);

			//act
			sut.Execute(obj);
			schedulers.AdvanceBy(200);

			//assert
			actual.Should().Be(expected);
		}
		public void Execute_WhenExuctingWhileOtherExecutionIsOccuring_ShouldCancelPreviousExecution(
		  [Frozen]TestSchedulers schedulers)
		{
			//arrange
			var firstObservable = schedulers.CreateColdObservable(OnNext(200, new object()), OnNext(300, new object()));
			var secondObservable = schedulers.CreateColdObservable(OnNext(400, new object()), OnNext(500, new object()));
			var i = 0;
			Func<object, IObservable<object>> observableFactory = _ => ++i == 1 ? firstObservable : secondObservable;
			var sut = new ObservableMvvmCommand<object, object>(observableFactory, schedulers, schedulers, "name", new AlwaysTrueCanExecuteStrategy<object>());

			//act
			sut.Execute(null);
			schedulers.AdvanceBy(250);
			sut.Execute(null);
			schedulers.AdvanceBy(350);

			//assert
			firstObservable.Subscriptions.Single().Unsubscribe.Should().Be(250);
		}
		public void Execute_ShouldBeCalledWithArg(
			IObservable<object> value,
			object arg,
			TestSchedulers schedulers)
		{
			//arrange
			object actual = null;
			var sut = new ObservableMvvmCommand<object, object>(o =>
			{
				actual = o;
				return value;
			},
			schedulers,
			schedulers,
			"name",
			new AlwaysTrueCanExecuteStrategy<object>());

			//act
			sut.Execute(arg);
			schedulers.AdvanceBy(200);

			//assert
			actual.Should().Be(arg);
		}
		public void Execute_ShouldCallCallback(
			object expected,
			TestSchedulers schedulers)
		{
			//arrange
			object actual = null;
			var value = Observable.Return(expected, ImmediateScheduler.Instance);
			var sut = new ObservableMvvmCommand<object, object>(
				o => value,
				schedulers,
				schedulers,
				"name",
				new AlwaysTrueCanExecuteStrategy<object>(),
				doObserver: () => Observer.Create<object>(o => actual = o),
				doScheduler: ImmediateScheduler.Instance);

			//act
			sut.Execute(null);
			schedulers.AdvanceBy(200);

			//assert
			actual.Should().Be(expected);
		}