.Net Aspire : Replacing a dependency in tests with Wiremock #660

Closed
opened 2025-12-29 08:32:01 +01:00 by adam · 3 comments
Owner

Originally created by @hibri on GitHub (Dec 31, 2024).

Originally assigned to: @StefH on GitHub.

I'm using .Net Aspire, and looking to replace an API dependency with Wiremock.
The project "api" calls a downstream service "ingredients", that I want to mock.

For example, in my test project, I have

IResourceBuilder<WireMockServerResource> mockApiService = _testingBuilder
            .AddWireMock("ingredients", WireMockServerArguments.DefaultPort )
            .WithApiMappingBuilder(adminApibuilder =>
            {
                adminApibuilder.Given(b => b
                    .WithRequest(r => r
                        .UsingGet()
                        .WithPath("/ingredients")
                    )
                    .WithResponse(r => r
                        .WithHeaders(h => h.Add("Content-Type", "application/json"))
                        .WithBodyAsJson(() => @"{""id"":1}")
                    ));
                return Task.FromResult(adminApibuilder);
            });

      _app = await _testingBuilder.BuildAsync();
        await _app.StartAsync();
        var resourceNotificationService = _app.Services.GetRequiredService<ResourceNotificationService>();

        await resourceNotificationService
            .WaitForResourceAsync("api", KnownResourceStates.Running)
            .WaitAsync(TimeSpan.FromSeconds(5));


I'm creating a http client for api, to create and send a request.

The code for the controller handling the request looks like this;

{
    [ApiController]
    [Route("[controller]")]
    public class RecipesController : ControllerBase
    {
   

        private readonly ILogger<RecipesController> _logger;
        private readonly HttpClient _httpClient;

        public RecipesController(ILogger<RecipesController> logger, IHttpClientFactory httpClientFactory)
        {
            _logger = logger;
            _httpClient = httpClientFactory.CreateClient("ingredients");
        }

        [HttpGet(Name = "GetRecipes")]
        public object Get()
        {
            return _httpClient.GetAsync("/api/recipes").Result;
        }
    }
}

I'm assuming that when the dependency for ingredients is resolved, we get a reference to the Wiremock instance. But this does not happen.
Hope this makes sense, or is this scenario not supported?

Originally created by @hibri on GitHub (Dec 31, 2024). Originally assigned to: @StefH on GitHub. I'm using .Net Aspire, and looking to replace an API dependency with Wiremock. The project "**api**" calls a downstream service "**ingredients**", that I want to mock. For example, in my test project, I have ``` C# IResourceBuilder<WireMockServerResource> mockApiService = _testingBuilder .AddWireMock("ingredients", WireMockServerArguments.DefaultPort ) .WithApiMappingBuilder(adminApibuilder => { adminApibuilder.Given(b => b .WithRequest(r => r .UsingGet() .WithPath("/ingredients") ) .WithResponse(r => r .WithHeaders(h => h.Add("Content-Type", "application/json")) .WithBodyAsJson(() => @"{""id"":1}") )); return Task.FromResult(adminApibuilder); }); _app = await _testingBuilder.BuildAsync(); await _app.StartAsync(); var resourceNotificationService = _app.Services.GetRequiredService<ResourceNotificationService>(); await resourceNotificationService .WaitForResourceAsync("api", KnownResourceStates.Running) .WaitAsync(TimeSpan.FromSeconds(5)); ``` I'm creating a http client for **api**, to create and send a request. The code for the controller handling the request looks like this; ``` C# { [ApiController] [Route("[controller]")] public class RecipesController : ControllerBase { private readonly ILogger<RecipesController> _logger; private readonly HttpClient _httpClient; public RecipesController(ILogger<RecipesController> logger, IHttpClientFactory httpClientFactory) { _logger = logger; _httpClient = httpClientFactory.CreateClient("ingredients"); } [HttpGet(Name = "GetRecipes")] public object Get() { return _httpClient.GetAsync("/api/recipes").Result; } } } ``` I'm assuming that when the dependency for **ingredients** is resolved, we get a reference to the Wiremock instance. But this does not happen. Hope this makes sense, or is this scenario not supported?
adam added the question label 2025-12-29 08:32:01 +01:00
adam closed this issue 2025-12-29 08:32:01 +01:00
Author
Owner

@StefH commented on GitHub (Dec 31, 2024):

I think that your api is not really using the wiremock - ingredients?

Can you also take a look here:
https://github.com/WireMock-Net/WireMock.Net/tree/master/examples-Aspire

@StefH commented on GitHub (Dec 31, 2024): I think that your api is not really using the wiremock - ingredients? Can you also take a look here: https://github.com/WireMock-Net/WireMock.Net/tree/master/examples-Aspire
Author
Owner

@hibri commented on GitHub (Jan 6, 2025):

Let me take another look. Thanks for replying on New Year's eve. Appreciate the fast response. Happy New Year

@hibri commented on GitHub (Jan 6, 2025): Let me take another look. Thanks for replying on New Year's eve. Appreciate the fast response. Happy New Year
Author
Owner

@StefH commented on GitHub (Jan 18, 2025):

Did you manage to get it working?

@StefH commented on GitHub (Jan 18, 2025): Did you manage to get it working?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#660