public void AddingItemMessageHandler_WhenInvoked_AddsNewItemToAdvertisementCollection()
        {
            //	Arrange
            var repository = GetNewspaperRepository();
            var message = new AddingAdvertisementItemMessage();
            var collectionViewModel = new AdvertisementCollectionViewModel(repository);
            collectionViewModel.Advertisements.Count.Should().Be(0, "There are no items in the collection initially.");
            //	Act
            Messenger.Default.Send(message);

            //	Assert
            collectionViewModel.Advertisements.Count.Should().Be(1, "A new item should have been added to the collection.");
            collectionViewModel.Advertisements.First().Name.Should().Be(Advertisement.MSG_NEW_ADVERTISEMENT_NAME,
                                                                        "The new item should be a new Advertisement.");
        }
        public void SelectedItemChangedHandler_WhenInvoked_SendsMessageAdvertisementChanged()
        {
            //	Arrange
            var messageReceived = false;
            AdvertisementItemViewModel vm = null;
            var repository = GetNewspaperRepository();
            var model1 = new Advertisement() {Name = "Ad 1", Text="Text 1"};
            var model2 = new Advertisement {Name = "Ad 2", Text="Text 2"};
            var adItemVm1 = new AdvertisementItemViewModel(repository) {Model = model1};
            var adItemVm2 = new AdvertisementItemViewModel(repository) {Model = model2};
            var collectionViewModel = new AdvertisementCollectionViewModel(repository);
            Messenger.Default.Register<CurrentAdvertisementItemChangedMessage>(this, (msg) =>
                                                                                 {
                                                                                     messageReceived = true;
                                                                                     vm = msg.ItemViewModel;
                                                                                 });
            var args = new RoutedPropertyChangedEventArgs<object>(null,adItemVm2);
            collectionViewModel.Advertisements.Add(adItemVm1);
            collectionViewModel.Advertisements.Add(adItemVm2);

            //	Act
            collectionViewModel.SelectedItemChangedCommand.Execute(args);

            //	Assert
            messageReceived.Should().Be(true, "The CurrentItemChangedMessage message was sent.");
            collectionViewModel.CurrentItem.Should().Be(adItemVm2, "The current item is the last one added.");
            vm.Model.Should().Be(adItemVm2.Model, "The added model was sent in the message.");
        }
        public void CancelCommand_WhenInvoked_ResetsNameToCollectionCurrentValue()
        {
            //	Arrange
            var repository = Substitute.For<INewspaperAdRepository>();
            repository.GetAllAdvertisements().Returns(new List<Advertisement>());
            var detailViewModel = new AdvertisementDetailViewModel(repository);
            var collectionViewModel = new AdvertisementCollectionViewModel(repository);
            detailViewModel.AddItemCommand.Execute(null);
            collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel,
                                                        "The detail model points to the Collection CurrentItem");
            var oldName = collectionViewModel.CurrentItem.Name;
            var newName = "Changed Advertisement Name";
            detailViewModel.Name = newName;
            collectionViewModel.CurrentItem.Name.Should().Be(oldName, "Name in the collection should not change");
            detailViewModel.Name.Should().Be(newName, "Alteration is reflected in the detail view model");

            //	Act
            detailViewModel.CancelItemCommand.Execute(null);

            //	Assert
            detailViewModel.Name.Should().Be(oldName, "The Advertisement name should revert back");
            collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel,
                                                        "The entire instance should have reverted back to the original value");
        }
        public void SaveCommand_WhenInvoked_UpdatesCollectionCurrentItemWithNewValue()
        {
            //	Arrange
            var repository = Substitute.For<INewspaperAdRepository>();
            repository.GetAllAdvertisements().Returns(new List<Advertisement>());
            var detailViewModel = new AdvertisementDetailViewModel(repository);
            var collectionViewModel = new AdvertisementCollectionViewModel(repository);
            detailViewModel.AddItemCommand.Execute(null);
            collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel,
                                                        "The detail model points to the Collection CurrentItem");
            var oldName = collectionViewModel.CurrentItem.Name;
            var newName = "Changed Advertisement Name";
            detailViewModel.Name = newName;
            var paperCount = detailViewModel.Newspapers.Count;

            //	Act
            detailViewModel.SaveItemCommand.Execute(null);

            //	Assert
            collectionViewModel.CurrentItem.Should().Be(detailViewModel.ItemViewModel,
                                                        "The current item in the collection matches the one in the detail view model.");
            detailViewModel.Newspapers.ToList().ForEach(n => repository.Received().Save(n.Model));
        }