public async Task ReturnDifferentResults_ForMultipleSearchMethodCall() { // Arrange InitializeServices(Configure); var viewModel = new ChuckNorrisSearchPageViewModel(); // Act viewModel.SearchTerm = "dog"; var firstQuotes = await viewModel.Quotes.Load(DefaultCancellationToken); viewModel.SearchTerm = "cat"; var secondQuotes = await viewModel.Quotes.Load(DefaultCancellationToken); // Assert using (new AssertionScope()) { firstQuotes.Length .Should().Be(3); secondQuotes.Length .Should().Be(1); } void Configure(IHostBuilder host) { host.ConfigureServices(services => { // This will replace the actual implementation of IApplicationSettingsService with a mocked version. ReplaceWithMock <IChuckNorrisService>(services, mock => { mock .Setup(m => m.Search(It.IsAny <CancellationToken>(), "dog")) .ReturnsAsync(new ChuckNorrisQuote[] { new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("1"), false), new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("3"), false), new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("1204"), false) }); mock .Setup(m => m.Search(It.IsAny <CancellationToken>(), "cat")) .ReturnsAsync(new ChuckNorrisQuote[] { new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("1"), false) }); MockingGetFavorites(mock); }); }); } }
public async Task ReturnEmptyCriterion_WhenProvidedSearchTermIsTooShort(string searchTerm) { // Arrange var viewModel = new ChuckNorrisSearchPageViewModel(); InitializeServices(ChuckNorrisSearchPageViewModelShould_Configuration); // Act viewModel.SearchTerm = searchTerm; var quotes = await viewModel.Quotes.Load(DefaultCancellationToken); // Assert quotes.Should().BeEmpty(); }
public async Task ReturnFavourited() { // Arrange var mockChuckNorrisService = new Mock <IChuckNorrisService>(); var searchViewModel = new ChuckNorrisSearchPageViewModel(); searchViewModel.SearchTerm = "dog"; var searchedQuotes = await searchViewModel.Quotes.Load(DefaultCancellationToken); var firstQuoteVm = searchedQuotes.First(); // Act await searchViewModel.ToggleIsFavorite.Execute(firstQuoteVm); await NavigateAndClear(DefaultCancellationToken, () => new ChuckNorrisFavoritesPageViewModel()); var favouritesViewModel = (ChuckNorrisFavoritesPageViewModel)GetCurrentViewModel(); var favouritedQuotes = await favouritesViewModel.Quotes.Load(DefaultCancellationToken) as ReadOnlyObservableCollection <ChuckNorrisItemViewModel>; // ReadOnlyObservableCollection does not load its content directly. Waiting for the CollectionChanged is a workaround to wait for its content to be loaded. var tcs = new TaskCompletionSource <Unit>(); // CollectionChanged is private for ReadOnlyObservableCollection, therefore casting it into INotifyCollectionChanged is a workaround to be able to access this event // https://stackoverflow.com/questions/2058176/why-is-readonlyobservablecollection-collectionchanged-not-public ((INotifyCollectionChanged)favouritedQuotes).CollectionChanged += OnFavouriteCollectionChanged; await tcs.Task; ((INotifyCollectionChanged)favouritedQuotes).CollectionChanged -= OnFavouriteCollectionChanged; // Assert favouritedQuotes.Should().NotBeNull(); var results = favouritedQuotes .Select(vm => vm.Quote); results.Should().Contain(firstQuoteVm.Quote); void OnFavouriteCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { try { tcs.TrySetResult(Unit.Default); } catch (Exception ex) { tcs.TrySetException(ex); } } }
public async Task CheckForSearchMethod_WhenQuotesAreLoading() { // Arrange InitializeServices(Configure); var viewModel = new ChuckNorrisSearchPageViewModel(); // Act viewModel.SearchTerm = "dog"; var quotes = await viewModel.Quotes.Load(DefaultCancellationToken); // Assert using (new AssertionScope()) { quotes.Should() .NotBeNull() .And .NotBeEmpty(); quotes.All(q => q.Quote.Value.ToUpperInvariant().Contains("DOG")) .Should().BeTrue("All quotes found searching for search term 'dog' should contain 'dog' "); } void Configure(IHostBuilder host) { host.ConfigureServices(services => { // This will replace the actual implementation of IApplicationSettingsService with a mocked version. ReplaceWithMock <IChuckNorrisService>(services, mock => { mock .Setup(m => m.Search(It.IsAny <CancellationToken>(), It.IsAny <string>())) .ReturnsAsync(new ChuckNorrisQuote[] { new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("0").WithValue("Something something dog"), false), new ChuckNorrisQuote(new ChuckNorrisData.Builder().WithId("1203").WithValue("Dog something"), false) }); MockingGetFavorites(mock); }); }); } }