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

Question: How to properly leverage the DistributedApplicationTestingBuilder.CreateAsync<TEntryPoint>() #7008

Closed
BenjaminCharlton opened this issue Jan 2, 2025 · 4 comments
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing
Milestone

Comments

@BenjaminCharlton
Copy link

I'm working on a new Aspire feature that I hope will be accepted as a Pull Request by the Aspire team.
(See an explanation and my progress here, if interested: #4785)

However, I'm struggling with my unit tests. I'm trying to use the CreateAsync method of the DistributedApplicationTestingBuilder to get a mock distributed application

var distributedApplicationBuilder = await DistributedApplicationTestingBuilder.CreateAsync<BlazorFluentUI_AppHost>();

It was working fine in my own test project in my own solution, where I was passing the type of a recently created AppHost project as the value of T. However, when I copied my unit tests into the Aspire solution (\tests\Aspire.Hosting.Tests) some of them started failing. I tried various values of T, including TestingAppHost1_AppHost (which is a distributed application host project already referenced by the Aspire.Hosting.Tests project.

In all cases above, I got the message:

System.InvalidOperationException : The entry point exited without building a DistributedApplication.

Please could you tell me what class I can use to bootstrap DistributedApplicationTestingBuilder to use in my unit tests?

Thanks for your help!

Benjamin

@davidfowl davidfowl added the area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing label Jan 9, 2025
@davidfowl
Copy link
Member

Can you share a sample?

@davidfowl davidfowl modified the milestone: Backlog Jan 10, 2025
@davidfowl davidfowl added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 10, 2025
@davidfowl davidfowl added this to the Backlog milestone Jan 10, 2025
@BenjaminCharlton
Copy link
Author

BenjaminCharlton commented Jan 12, 2025

Hi @davidfowl! Happy New Year! Sure, how about this test in the file linked here. The method named BlazorResourceBuilderExtensionsTests.AddWebAssemblyClient_WithoutConfigure_InvokesDefaultConfiguration

The first line of that test is:

 var distributedApplicationBuilder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.Aspire4Wasm_DummyApp_AppHost>();

And it throws:

System.InvalidOperationException : The entry point exited without building a DistributedApplication.

The type that I pass into the type argument TEntryPoint is the app host of an unedited Aspire template app Projects.Aspire4Wasm_DummyApp_AppHost, which is referenced by the unit test project.

Could you let me know if that's how DistributedApplicationTestingBuilder was supposed to be used, please? I wondered what I'd done to make it fail. I also wondered if linking a complete dummy app was unnecessary, and maybe there was a lightweight class that could be used as the TEntryPoint for testing purposes?

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 12, 2025
@davidfowl
Copy link
Member

var distributedApplicationBuilder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.Aspire4Wasm_DummyApp_AppHost>();

The type that I pass into the type argument TEntryPoint is the app host of an unedited Aspire template app Projects.Aspire4Wasm_DummyApp_AppHost, which is referenced by the unit test project.

Could you let me know if that's not how DistributedApplicationTestingBuilder was supposed to be used, please?

No that's not correct. The entry point must be a type in the apphost's assembly (usually the Program type). The DistributedApplicationTestingBuilder runs the apphost project. The type is used to find the entryponit assembly. I know Projects.XX looks like it should work, but that's the generated class used by the apphost to avoid hard coding project paths.

@BenjaminCharlton
Copy link
Author

Thanks @davidfowl that got all those tests passing! Much appreciate the help.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing
Projects
None yet
Development

No branches or pull requests

3 participants