Integration of asp.net core testing with authentication providers

I am new to asp.net core. I am currently developing a small site that uses authentication providers from Google, Facebook and Microsoft.

Now I wanted to write some integration tests for mine HomeController.

This is what my test class looks like:

public class HomeControllerTests : IDisposable
{
    private HomeController _homeController;
    private readonly TestServer _server;
    private readonly HttpClient _client;

    public HomeControllerTests()
    {
        _server = new TestServer(new WebHostBuilder().UseEnvironment("Development").UseStartup<Startup>());
        _client = _server.CreateClient();
    }

    [Fact]
    public async void Test()
    {
        var result = await _client.GetAsync("/");

        Assert.NotNull(result);
    }

    public void Dispose()
    {
        _server.Dispose();
        _client.Dispose();
    }
}

Now in my startup.cs on

builder.AddUserSecrets();

throws an InvalidOperationException:

An exception of type "System.InvalidOperationException" occurred in Microsoft.Extensions.Configuration.UserSecrets.dll, but was not processed in the user code.

Can someone point me in the right direction, what could be wrong? Will tests run on the build server?

+4
2

, , . @Gevorg , . StackOverflow, .

Programs.cs

public class Program
{
    public static void Main(string[] args)
    {
        var host = GetHostBuilder(args)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .Build();

        host.Run();
    }

    public static IWebHostBuilder GetHostBuilder(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        return new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseStartup<Startup>();
    }
}

:

public HomeControllerTests()
    {
        var basePath = PlatformServices.Default.Application.ApplicationBasePath;
        var projectPath = Path.GetFullPath(Path.Combine(basePath, "../../../../WebProjectNameYouWantToTest"));

        _server = new TestServer(Program.GetHostBuilder(new string[] {}).UseContentRoot(projectPath).UseEnvironment("Development").UseStartup<Startup>());
        _client = _server.CreateClient();
    }

.

- , , .

+1

builder.AddUserSecrets() userSecretsId project.json, . TestStartup, .

_server = new TestServer(new WebHostBuilder()
    .UseEnvironment("Development")
    .UseStartup<TestStartup>());

class TestStartup
{
    public TestStartup (IHostingEnvironment env)
    {
      ...
    }
}

, .

_server = new TestServer(Program.GetHostBuilder()
    .UseContentRoot(@"YourFullPathToProjectWhichyoutest"));
_client = _server.CreateClient();

, , program.cs

public static void Main(string[] args)
{
    var host = GetHostBuilder()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .Build();

    host.Run();
}
//move your common configurations here
public static IWebHostBuilder GetHostBuilder()
{
    return new WebHostBuilder()
        .UseKestrel()
        .UseStartup<Startup>();
}
+1

Source: https://habr.com/ru/post/1657870/


All Articles