public void WhenReceivesContacts_ThenAddsContactsToTheCollection() { var contactsServiceMock = new Mock<IContactsService>(); AsyncCallback callback = null; var resultMock = new Mock<IAsyncResult>(); contactsServiceMock .Setup(svc => svc.BeginGetContacts(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((cb, s) => callback = cb) .Returns(resultMock.Object); var contacts = new[] { new Contact { }, new Contact { } }; contactsServiceMock .Setup(svc => svc.EndGetContacts(resultMock.Object)) .Returns(contacts); var regionManager = new RegionManager(); var viewModel = new ContactsViewModel(contactsServiceMock.Object, regionManager); this.EnqueueConditional(() => callback != null); this.EnqueueCallback(() => callback(resultMock.Object)); this.EnqueueCallback( () => { CollectionAssert.AreEqual(contacts, new List<Contact>(viewModel.Contacts.Cast<Contact>())); }); this.EnqueueTestComplete(); }
public ContactsView(IRegionManager regionManager, ContactsViewModel viewModel) { this.regionManager = regionManager; InitializeComponent(); this.ViewModel = viewModel; }
public void WhenCreated_ThenRequestsContacts() { var contactsServiceMock = new Mock<IContactsService>(); var regionManager = new RegionManager(); var viewModel = new ContactsViewModel(contactsServiceMock.Object, regionManager); contactsServiceMock.Verify(svc => svc.GetContactsAsync()); Assert.IsTrue(viewModel.Contacts.IsEmpty); }
public void WhenContactIsSelected_ThenEmailContactCommandIsEnabledAndNotifiesChange() { var contactsServiceMock = new Mock<IContactsService>(); AsyncCallback callback = null; var resultMock = new Mock<IAsyncResult>(); contactsServiceMock .Setup(svc => svc.BeginGetContacts(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((cb, s) => callback = cb) .Returns(resultMock.Object); var contacts = new[] { new Contact { }, new Contact { } }; contactsServiceMock .Setup(svc => svc.EndGetContacts(resultMock.Object)) .Returns(contacts); var regionManager = new RegionManager(); var viewModel = new ContactsViewModel(contactsServiceMock.Object, regionManager); this.EnqueueConditional(() => callback != null); this.EnqueueCallback(() => callback(resultMock.Object)); this.EnqueueCallback( () => { var notified = false; viewModel.EmailContactCommand.CanExecuteChanged += (s, o) => notified = true; Assert.IsFalse(viewModel.EmailContactCommand.CanExecute(null)); viewModel.Contacts.MoveCurrentToFirst(); Assert.IsTrue(viewModel.EmailContactCommand.CanExecute(null)); Assert.IsTrue(notified); }); this.EnqueueTestComplete(); }
public void WhenSendingEmail_ThenNavigatesWithAToQueryParameter() { var contactsServiceMock = new Mock<IContactsService>(); AsyncCallback callback = null; var resultMock = new Mock<IAsyncResult>(); contactsServiceMock .Setup(svc => svc.BeginGetContacts(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((cb, s) => callback = cb) .Returns(resultMock.Object); var contacts = new[] { new Contact { EmailAddress = "email" }, new Contact { } }; contactsServiceMock .Setup(svc => svc.EndGetContacts(resultMock.Object)) .Returns(contacts); Mock<IRegion> regionMock = new Mock<IRegion>(); regionMock .Setup(x => x.RequestNavigate(new Uri("ComposeEmailView?To=email", UriKind.Relative), It.IsAny<Action<NavigationResult>>())) .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true))) .Verifiable(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true); regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object); IRegionManager regionManager = regionManagerMock.Object; var viewModel = new ContactsViewModel(contactsServiceMock.Object, regionManager); this.EnqueueConditional(() => callback != null); this.EnqueueCallback(() => callback(resultMock.Object)); this.EnqueueCallback( () => { viewModel.Contacts.MoveCurrentToFirst(); viewModel.EmailContactCommand.Execute(null); regionMock.VerifyAll(); }); this.EnqueueTestComplete(); }