public async Task Test1() { Mock <ISleeperApiClient> mockClient = new Mock <ISleeperApiClient>(); var factory = new TestWebApplicationFactory <Startup>(services => { // setup the swaps services.SwapTransient <ISleeperApiClient>(provider => mockClient.Object); }); mockClient.Setup(x => x.GetUserAsync(It.IsAny <string>())).ReturnsAsync(new SleeperUser { Name = "My Moq User" }); // Create an HttpClient which is setup for the test host var client = factory.CreateClient(); // Make a call to the base route HttpResponseMessage response = await client.GetAsync($"/api/sleeper/anyvalue"); // Assert we got the successful response Assert.That(response.IsSuccessStatusCode, Is.True); // Assert the content is as expected string responseContent = await response.Content.ReadAsStringAsync(); SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent); Assert.That(sleeperUser.Name, Is.EqualTo("My Moq User")); mockClient.Verify(x => x.GetUserAsync(It.IsAny <string>()), Times.Once); }
public async Task Test1() { // Configure the Host Builder IHostBuilder hostBuilder = new HostBuilder() .ConfigureWebHost(webHost => { // Add TestServer webHost.UseTestServer(); // Specify the environment webHost.UseStartup <TestServerPlayground.Startup>(); }); // Build and start the Host IHost host = await hostBuilder.StartAsync(); // Create an HttpClient to send requests to the TestServer HttpClient client = host.GetTestClient(); string username = "******"; // Make a call to the base route HttpResponseMessage response = await client.GetAsync($"/api/sleeper/{username}"); // Assert we got the successful response Assert.That(response.IsSuccessStatusCode, Is.True); // Assert the content is as expected string responseContent = await response.Content.ReadAsStringAsync(); SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent); Assert.That(sleeperUser.Name, Is.EqualTo(username)); }
public async Task Test2() { // Configure the Host Builder IHostBuilder hostBuilder = new HostBuilder() .ConfigureWebHost(webHost => { // Add TestServer webHost.UseTestServer(); // Specify the environment webHost.UseStartup <TestServerPlayground.Startup>(); webHost.ConfigureTestServices(services => { //Remove SleeperApiClient services.Remove(services.Single(x => x.ServiceType == typeof(ISleeperApiClient))); //Add MockSleeperApiClient services.AddTransient(typeof(ISleeperApiClient), typeof(MockSleeperApiClient)); }); }); // Build and start the Host IHost host = await hostBuilder.StartAsync(); // Create an HttpClient to send requests to the TestServer HttpClient client = host.GetTestClient(); // Make a call to the base route HttpResponseMessage response = await client.GetAsync($"/api/sleeper/anyvalue"); // Assert we got the successful response Assert.That(response.IsSuccessStatusCode, Is.True); // Assert the content is as expected string responseContent = await response.Content.ReadAsStringAsync(); SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent); Assert.That(sleeperUser.Name, Is.EqualTo("Test User")); }
public async Task Test2() { var factory = new TestWebApplicationFactory <Startup>(services => { // setup the swaps services.SwapTransient <ISleeperApiClient, MockSleeperApiClient>(); }); // Create an HttpClient which is setup for the test host var client = factory.CreateClient(); // Make a call to the base route HttpResponseMessage response = await client.GetAsync("/api/sleeper/anyvalue"); // Assert we got the successful response Assert.That(response.IsSuccessStatusCode, Is.True); // Assert the content is as expected string responseContent = await response.Content.ReadAsStringAsync(); SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent); Assert.That(sleeperUser.Name, Is.EqualTo("Test User")); }