Пример #1
0
        public void errors_saving_data_are_surfaced()
        {
            new TestScheduler().With(scheduler =>
            {
                var api = new ApiMock();
                api
                .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>()))
                .Return(Observable.Throw <Unit>(new InvalidOperationException("foo")));
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();
                sut.Name   = "Barney";
                sut.Weight = "42";
                scheduler.AdvanceByMs(2001);

                Assert.NotNull(sut.Error);
                Assert.Equal("foo", sut.Error.Exception.Message);

                api
                .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>()))
                .Return(Observable.Return(Unit.Default));
                sut
                .Error
                .RetryCommand
                .Execute(null);
                scheduler.AdvanceByMs(1);
                Assert.Null(sut.Error);
            });
        }
Пример #2
0
        public void errors_retrieving_data_are_surfaced()
        {
            CurrentThreadScheduler.Instance.With(scheduler =>
            {
                var api = new ApiMock();
                api
                .When(x => x.GetDinosaur(It.IsAny <int>()))
                .Return(Observable.Throw <Dinosaur>(new InvalidOperationException("foo")));
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();

                Assert.NotNull(sut.Error);
                Assert.Equal("foo", sut.Error.Exception.Message);

                api
                .When(x => x.GetDinosaur(It.IsAny <int>()))
                .Return(Observable.Return(default(Dinosaur)));
                sut
                .Error
                .RetryCommand
                .Execute(null);
                Assert.Null(sut.Error);
            });
        }
Пример #3
0
        public void retrieved_data_is_used_to_populate_the_initial_values()
        {
            new TestScheduler().With(scheduler =>
            {
                var dinosaur = new Dinosaur("Barney", 13, new byte[] { 1, 2, 3 });
                var api      = new ApiMock();
                api
                .When(x => x.GetDinosaur(42))
                .Return(Observable.Return(dinosaur));
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();
                scheduler.AdvanceByMs(1);

                Assert.Equal("Barney", sut.Name);
                Assert.Equal("13", sut.Weight);
                Assert.Equal(13, sut.ValidatedWeight.Value);
                Assert.NotNull(sut.Image);
            });
        }
Пример #4
0
        public void busy_flag_remains_true_whilst_deleting_data()
        {
            CurrentThreadScheduler.Instance.With(scheduler =>
            {
                var api            = new ApiMock();
                var deleteDinosaur = new Subject <Unit>();
                api
                .When(x => x.DeleteDinosaur(It.IsAny <int>()))
                .Return(deleteDinosaur);
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();

                using (sut.ConfirmDeleteInteraction.RegisterHandler(context => context.SetOutput(true)))
                {
                    sut
                    .DeleteCommand
                    .Execute()
                    .Subscribe();
                    Assert.True(sut.IsBusy);
                }

                deleteDinosaur.OnCompleted();
                Assert.False(sut.IsBusy);
            });
        }
Пример #5
0
        public void busy_flag_remains_true_whilst_saving_data()
        {
            new TestScheduler().With(scheduler =>
            {
                var api          = new ApiMock();
                var saveDinosaur = new Subject <Unit>();
                api
                .When(x => x.SaveDinosaur(It.IsAny <Dinosaur>()))
                .Return(saveDinosaur);
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();

                sut.Name   = "Barney";
                sut.Weight = "42";
                scheduler.AdvanceByMs(2001);
                Assert.True(sut.IsBusy);

                saveDinosaur.OnCompleted();
                scheduler.AdvanceByMs(1);
                Assert.False(sut.IsBusy);
            });
        }
Пример #6
0
        public void data_cannot_be_deleted_if_the_auditing_system_is_unavailable()
        {
            var api = new ApiMock();
            var isAuditingAvailable = new BehaviorSubject <bool>(true);

            api
            .When(x => x.IsAuditingAvailable)
            .Return(isAuditingAvailable);
            var sut = new DinosaurDetailsViewModel(
                42,
                new BitmapLoaderMock(),
                api);

            sut
            .Activator
            .Activate();

            Assert.True(sut.DeleteCommand.CanExecute.FirstAsync().Wait());

            isAuditingAvailable.OnNext(false);
            Assert.False(sut.DeleteCommand.CanExecute.FirstAsync().Wait());

            isAuditingAvailable.OnNext(true);
            Assert.True(sut.DeleteCommand.CanExecute.FirstAsync().Wait());
        }
Пример #7
0
        public void errors_deleting_data_are_surfaced()
        {
            CurrentThreadScheduler.Instance.With(scheduler =>
            {
                var api = new ApiMock();
                api
                .When(x => x.DeleteDinosaur(It.IsAny <int>()))
                .Return(Observable.Throw <Unit>(new InvalidOperationException("foo")));
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();

                using (sut.ConfirmDeleteInteraction.RegisterHandler(context => context.SetOutput(true)))
                {
                    sut
                    .DeleteCommand
                    .Execute()
                    .Subscribe(
                        _ => { },
                        _ => { });

                    Assert.NotNull(sut.Error);
                    Assert.Equal("foo", sut.Error.Exception.Message);

                    api
                    .When(x => x.DeleteDinosaur(It.IsAny <int>()))
                    .Return(Observable.Return(Unit.Default));
                    sut
                    .Error
                    .RetryCommand
                    .Execute(null);
                    Assert.Null(sut.Error);
                }
            });
        }
Пример #8
0
        public void memory_is_reclaimed_after_deactivation()
        {
            //
            // IMPORTANT: if you're observing failures in this test, try running it in release mode without a debugger attached.
            //

            CurrentThreadScheduler.Instance.With(scheduler =>
            {
                // The SUT will subscribe to this subject, so we hold a reference to it so that it can't be GC'd.
                var isAuditingAvailable = new Subject <bool>();
                var api = new ApiMock();
                api
                .When(x => x.IsAuditingAvailable)
                .Return(isAuditingAvailable);
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);
                sut
                .Activator
                .Activate();

                var weakSut = new WeakReference(sut);
                sut         = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.True(weakSut.IsAlive);

                // Deactivation is when the SUT should detach subscriptions and therefore become eligible for collection.
                ((DinosaurDetailsViewModel)weakSut.Target)
                .Activator
                .Deactivate();

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Assert.False(weakSut.IsAlive);
            });
        }
Пример #9
0
        public void busy_flag_remains_true_whilst_retrieving_data()
        {
            CurrentThreadScheduler.Instance.With(scheduler =>
            {
                var api         = new ApiMock();
                var getDinosaur = new Subject <Dinosaur>();
                api
                .When(x => x.GetDinosaur(It.IsAny <int>()))
                .Return(getDinosaur);
                var sut = new DinosaurDetailsViewModel(
                    42,
                    new BitmapLoaderMock(),
                    api);

                sut
                .Activator
                .Activate();
                Assert.True(sut.IsBusy);

                getDinosaur.OnCompleted();
                Assert.False(sut.IsBusy);
            });
        }