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

Simplify Dependency Injection Setup #150

Closed
ivanpaulovich opened this issue Jan 28, 2020 · 5 comments
Closed

Simplify Dependency Injection Setup #150

ivanpaulovich opened this issue Jan 28, 2020 · 5 comments

Comments

@ivanpaulovich
Copy link
Owner

I'd like to simplify Dependency Injection of Modules that fallback on each other, For instance, the Entity Framework is replaced by the In Memory Fakes.

It would be nice to make Persistence module smarter to switch from Real Mode to Test Mode.

@douglasramos
Copy link

I'd like to help. Can you tell more about what we're trying to solve here?
We could customize the WebApplicationFactory for changing database setup during tests. More specifically, on integration tests. (This link tell us more about it: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1). Particularly, the way that it is now, what we're calling integration tests are actually a unit test of the repository with some fake dbContext. And, what we're calling component test is actually the integration test (at least for asp net core context)

@ivanpaulovich
Copy link
Owner Author

Hey again @douglasramos 😄

The WebApplicationFactory would make the Component/Integration Tests easier to setup. It's a good idea to see it implemented here.

My initial thoughts was to design the dependency injection of "Real Implementations" like Entity Framework fallback into "In Memory" if it is running on test mode.

Also, features like Swagger, Authentication, API Versioning need some love segregating them.

@douglasramos
Copy link

Hi, @ivanpaulovich

Hmm, interesting. I can't think of a way to build this "switcher" inside of the WebAPI project, though. At least not an elegant way. How would the server itself have the knowledge that it is under test?

The way I see, the approach would be the WebApplicationFactory act as the switcher, changing EF Core implementation to in-memory fake ones. Unfortunately, that doesn’t cover unit test cases.

Let me know if you have any more thoughts on this :)

ivanpaulovich added a commit that referenced this issue Feb 18, 2020
ivanpaulovich added a commit that referenced this issue Feb 18, 2020
@ivanpaulovich
Copy link
Owner Author

Hi @douglasramos,

Sorry for the delay in answering this topic.

Did you see the new CustomWebApplicationFactory.cs? The current implementation tells the WebAPI to UseFake and it loads the in-memory implementation.

public sealed class CustomWebApplicationFactory : WebApplicationFactory<Startup>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder) => builder.ConfigureAppConfiguration(
        (context, config) =>
        {
            config.AddInMemoryCollection(
                new Dictionary<string, string> {["PersistenceModule:UseFake"] = "true"});
        }).ConfigureServices(services =>
    {
        services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = "Test";
                x.DefaultChallengeScheme = "Test";
            })
            .AddScheme<AuthenticationSchemeOptions, TestAuthenticationHandler>(
                "Test", options => { });
    });
}

@ivanpaulovich
Copy link
Owner Author

Closing as topic is too old.

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

No branches or pull requests

2 participants