Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hibri opened this issue Dec 31, 2024 · 3 comments
Open

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

hibri opened this issue Dec 31, 2024 · 3 comments
Assignees
Labels

Comments

@hibri
Copy link

hibri commented Dec 31, 2024

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?

@hibri hibri added the question label Dec 31, 2024
@StefH StefH self-assigned this Dec 31, 2024
@StefH
Copy link
Collaborator

StefH commented 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

@hibri
Copy link
Author

hibri commented Jan 6, 2025

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

@StefH
Copy link
Collaborator

StefH commented Jan 18, 2025

Did you manage to get it working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants