简体   繁体   中英

Integration testing an asp.net core with authentication providers

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

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

This is how 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 at

builder.AddUserSecrets();

an InvalidOperationException gets thrown:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.Configuration.UserSecrets.dll but was not handled in user code.

Can anybody point me to the right direction what could be wrong? Will the tests run on the build server as well?

builder.AddUserSecrets() reads your userSecretsId from project.json file, which is not accessible from test project. You can run your test server by another TestStartup Class which does implement only testable configuration.

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

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

If you need to test your real project with all related configuration do following.

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

in project which you are going to test change program.cs file as here

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>();
}

I finally had the time have a look at my problem. Thanks to @Gevorg Narimanyan for pushing me into the right direction. I found a post here on StackOverflow that helped me as well.

Here is my 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>();
    }
}

And this is how the setup looks like in my test class:

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();
    }

With this setup you can test your project without any problems.

If you find any issues or you think that this is wrong, don't hesitate to correct me.

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