简体   繁体   中英

Asp.Net Core 3.0 trouble with Api Integration Test

I configure AutoFac for Asp.Net Core 3.0 but now i have trouble with ControllerTestBase class. I know I shouldn't use WebHostBuilder and write other code but I tried many times. I only got new errors.

ControllerTestBase.cs

namespace Passenger.Tests.EndToEnd.Controllers
{
    public abstract class ControllerTestsBase
    {
        protected readonly TestServer Server;
        protected readonly HttpClient Client;

        protected ControllerTestsBase()
        {
            var hostBuilder = new HostBuilder()
              .ConfigureWebHost(webHost =>
              {
                // Add TestServer
                webHost.UseTestServer();
                webHost.UseStartup<Startup>();
              });

            var host = hostBuilder.Start();
            Client = host.GetTestClient();

        }

        protected static StringContent GetPayload(object data)
        {
            var json = JsonConvert.SerializeObject(data);

            //Content-Type: "application/json"
            return new StringContent(json, Encoding.UTF8, "application/json");
        }


    }
}


AccountControllerTest.cs


namespace Passenger.Tests.EndToEnd.Controllers
{
    public class AccountControllerTests : ControllerTestsBase
    {

        [Fact]
        public async Task given_valid_current_and_new_password_it_should_be_changed()
        {
            // Act

            var command = new ChangeUserPassword
            {
                CurrentPassword = "secret",
                NewPassword = "secret2"
            };
            var payload = GetPayload(command);
            var response = await Client.PutAsync("account/password", payload);
            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.NoContent);
        } 
    }
}

Error Message: https://pastebin.com/9jMdJkXC

You dont need to use Autofac as .net core 3.0 provides built-in dependency injection.

Read more about Dependency Injection in Asp.net Core .

Refactor your code and use dependency injection for object initialization.

    namespace Passenger.Tests.EndToEnd.Controllers
  {
     public class ControllerTestsBase
    {
    private readonly ITestServer _testServer;
    private readonly HttpClient Client;

    protected ControllerTestsBase(ITestServer testServer,HttpClient client)
    {
        _testServer = testServer;
         client.BaseAddress = new Uri("https://www.stackoverflow.com");

         client.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");

         client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample");

         Client = client;
    }

    protected static StringContent GetPayload(object data)
    {
        var json = JsonConvert.SerializeObject(data);

        //Content-Type: "application/json"
        return new StringContent(json, Encoding.UTF8, "application/json");
    }


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM