public async Task PrintsWeatherConditionsAndSavesToStorage() { var args = "weather --city Vilnius,Athens,Tbilisi,Yerevan".Split(' '); using var application = new CommandLineApplication(args); var weatherData = new WeatherConditions[] { new("Vilnius", -10.5, 11, "Cloudy"), new("Athens", 18.7, 2, "Sunny"), new("Tbilisi", 15.1, 4, "Sunny"), new("Yerevan", 25.9, 25, "Rainy") }; var handler = application.FakeHttpClientHandler; var callToAuthorize = A.CallTo(() => handler.SendAsyncOverride( A <HttpRequestMessage> .That.Matches(message => message.Method == HttpMethod.Post && message.RequestUri !.ToString().EndsWith("api/authorize")), A <CancellationToken> ._)); callToAuthorize.ReturnsLazily((HttpRequestMessage _, CancellationToken _) => { // ReSharper disable once ConvertToLambdaExpression return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{""bearer"":""1234567890""}") }); }); var callToGetWeatherData = A.CallTo(() => handler.SendAsyncOverride( A <HttpRequestMessage> .That.Matches(message => message.Method == HttpMethod.Get && message.Headers.Authorization !.Scheme == "bearer" && message.Headers.Authorization !.Parameter == "1234567890"), A <CancellationToken> ._)); callToGetWeatherData.ReturnsLazily((HttpRequestMessage m, CancellationToken _) => { var city = m.RequestUri !.PathAndQuery.Split('/').Last(); var data = weatherData.Single(w => w.City == city); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(data)) }); }); // Let's give enough time to complete the work of the background service. application.CancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(2)); var returnCode = await application.RunAsync(); Assert.Equal(0, returnCode); callToAuthorize.MustHaveHappened(4, Times.Exactly); callToGetWeatherData.MustHaveHappened(4, Times.Exactly); await AssertDatabase(weatherData); AssertConsoleOutput(application.StandardOutput); }