public async Task AutoInjection_MockAndVerify() { // Given var requestTrackerOne = m_TeePeeBuilderOne.ForRequest("https://unittest.example.typed/path/resource", HttpMethod.Put) .ContainingQueryParam("filter", "other") .WithBody(new { Caller = "ThisCaller" }) .Responds() .WithStatus(HttpStatusCode.Created) .TrackRequest(); var requestTrackerTwo = m_TeePeeBuilderTwo.ForRequest("https://unittest.anotherexample.typed/path/otherresource", HttpMethod.Put) .ContainingQueryParam("filter", "other") .WithBody(new { Caller = "ThisCaller" }) .Responds() .WithStatus(HttpStatusCode.Created) .TrackRequest(); var controller = Resolve.WithTypedClients <HttpClientFactoryMultipleTypedUsageController, ExampleTypedHttpClient, AnotherExampleTypedHttpClient>(m_TeePeeBuilderOne, m_TeePeeBuilderTwo, sc => { var configuration = UnitTestConfig.LoadUnitTestConfig(); // Call your production code, which sets up the Typed Client, here sc.AddTypedHttpClients(configuration); }); // When var result = await controller.FireAndForget(); // Then Assert.NotNull(result); Assert.IsType <OkResult>(result); requestTrackerOne.WasCalled(1); requestTrackerTwo.WasCalled(1); }
public async Task AutoInjection_RecommendedPassiveMocking() { // Given m_TeePeeBuilderOne.ForRequest("https://unittest.multipleone.named/pathone/resourceone", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); m_TeePeeBuilderTwo.ForRequest("https://unittest.multipletwo.named/pathtwo/resourcetwo", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 30 } } }); var controller = Resolve.WithNamedClients <HttpClientFactoryMultipleNamedUsageController>(sc => { var configuration = UnitTestConfig.LoadUnitTestConfig(); // Call your production code, which sets up the Typed Client, here sc.AddNamedHttpClients(configuration); }, m_TeePeeBuilderOne, m_TeePeeBuilderTwo); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(40, resultValue); }
public async Task AutoInjection_RecommendedPassiveMocking() { // Given m_TeePeeBuilder.ForRequest("https://unittest.example.typed/path/resource", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); var controller = Resolve.WithTypedClient <HttpClientFactoryTypedUsageController, ExampleTypedHttpClient>(m_TeePeeBuilder, sc => { /* Example of using prod Setup code */ var configuration = UnitTestConfig.LoadUnitTestConfig(); // Call your production code, which sets up the Typed Client, here sc.AddTypedHttpClients(configuration); }); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(10, resultValue); }