4

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How do you handle the Authorize attribute?
1

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

2 Comments

Do I have to copy the whole Startup class into this new TestStartup? Or do you have a complete example? For me it doesn't look like a real test of your environment then.
@ĐorđePetrović So sorry that it takes so long to edit my answer, now you can test it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.