Integration Test . Net Core 2.2

Asked

Viewed 515 times

2

I’m creating an integration test using microsoft Tuto Integration tests in ASP.NET Core

public class WebAppFactory : WebApplicationFactory<Startup>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            // Create a new service provider.
            var serviceProvider = new ServiceCollection()
                .AddEntityFrameworkInMemoryDatabase()
                .BuildServiceProvider();

            // Add a database context (ApplicationDbContext) using an in-memory 
            // database for testing.
            services.AddDbContext<AsiaCabIdentityContext>(options =>
            {
                options.UseInMemoryDatabase("InMemoryDbForTesting");
                options.UseInternalServiceProvider(serviceProvider);
            });

        });
    }
}

This is my test class

public class IntegrationTest : IClassFixture<WebApplicationFactory<Startup>>
{
    private readonly WebApplicationFactory<Startup> _factory;

    public IntegrationTest(WebApplicationFactory<Startup> factory)
    {
        _factory = new WebApplicationFactory<Startup>();
    }


    [Fact]
    public void Teste_Ok()
    {

        // Arrange
        var httpClient = _factory.CreateClient();


        var result = httpClient.GetAsync("/api/v1/user").Result;
    }
}

When I try to run the test I get this error :

System.Reflection.Targetinvocationexception Hresult=0x80131604 Message=Exception has been thrown by the target of an Invocation. Source=System.Private.Corelib Stacktrace: At system.RuntimeMethodHandle.Invokemethod(Object target, Object[] Arguments, Signature Sig, Boolean constructor, Boolean wrapExceptions) At system.Reflection.Runtimemethodinfo.Invoke(Object obj, Bindingflags invokeAttr, Binder Binder, Object[] Parameters, Cultureinfo Culture) At microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Webhostfactoryresolver.<>c__DisplayClass2_02.<ResolveWebHostBuilderFactory>b__0(String[] args) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.Createwebhostbuilder() At Microsoft.AspNetCore.Mvc.Testing.Webapplicationfactory1.EnsureServer() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.Createdefaultclient(Delegatinghandler[] handlers) At microsoft.AspNetCore.Mvc.Testing.Webapplicationfactory`1.Createclient(Webapplicationfactoryclientoptions options) At Identity.Tests.Integration.PageTest.Teste_ok() in C: test Identity.Tests Integration Pagetest.Cs:line 22

Inner Exception 1: Filenotfoundexception: Could not load file or Assembly 'Microsoft.AspNetCore.Server.Kestrel, Version=2.2.0.0, Culture=neutral, Publickeytoken=adb9793829ddae60'. System cannot find specified file.

I already installed Kestrel in the framework

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="Bogus" Version="27.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

1 answer

1

I had a similar problem, but he accused not finding the lib Microsoft.AspNetCore.App. What I did to resolve was to add the import of that lib to my csproj.

So in your case, I would try to add:

<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.