1

I am using ASP.NET 5 RC1 and I need to write integration tests ...

So on the Test project, ASPNET5_WEB_TEST, I have the following:

public class IntegrationTests {

  private readonly TestServer _server;
  private readonly HttpClient _client;

  public IntegrationTests() {

    _server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>());
    _client = _server.CreateClient();

  }

  // Test methods ...
}

The Startup class is from the ASP.NET 5 project I am testing: ASPNET5_WEB

When I run the test I get the following error:

The configuration file 'C:\Projects\ASPNET5_TEST\config.json' was not found and is not optional.

I know I get this error because on Startup I have:

  builder
    .AddJsonFile("config.json", false)
    .AddJsonFile($"config.{environment.EnvironmentName}.json", true);

To fix this error I need to copy, at least, config.json from my web project, ASPNET5_WEB, to my test project, ASPNET5_WEB_TEST. But this means I will need to maintain duplicate config.json or at least copy it every time I make a change.

Can't I tell TestServer to use Startup of the web project and also its config.*.json files?

And can I have a config.testing.json and set on the TestServer the environment to Testing so the Startup code uses config.json and config.testing.json?

2
  • Can you please give more details about the error? Who's throwing it? What do you have in the Startup class? It's just a guess but probably you have code in Startup that expects a config.json file Commented Feb 21, 2016 at 8:28
  • @VictorHurdugaci Yes, it is true that Startup expects config.json ... But can't I make the TestServer to use the config.json of the Web project and add an extra config.testing.json to it? I just updated my question ... Does it help to clarify things? Commented Feb 21, 2016 at 22:13

2 Answers 2

1

I assume you're using the TestServer from aspnet, if so, it wasn't built to support the way you're config files are read. The TestServer is used to run simple integration tests for their "hosting engine" but not for integrations tests for a website.

Their ApplicationDeployerFactory class is what you can use however. Refer to this as an example of how to run an "integration" server. I've used selenium in conjunction with that to run integration tests against the project I'm working on atm.

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

9 Comments

On my web project i also have an api. I am not using a self hosting api. So i need to test it to ... but even if it was a self hosted api does not also have config.json? What do you mean the way config files are read? Is there another way?
@Miguel What do you mean by self hosting api? Another way to get your TestServer working is to pass an IConfiguration instance to the TestServer's constructor. You'll have to build it yourself when setting up your test though and you'll probably need something in your Startup that tells it not to read the config.json files if you're testing.
If you're testing "simple" stuff then that article should be sufficient. You'll find that if you have a more complicated project that it's better to use the ApplicationDeployerFactory though. If you could add one of your test methods, that'll probably be helpful as well.
Use TestServer to test simple responses. If you are trying to do a full integration testing I recommend you check the SO link in my answer. That runs a standalone kestrel server (the url you can control of course), write selenium tests to test html or just use HttpClient if you want to test json responses from your API. The ApplicationDeployerFactory will run your website the way you're expecting it to. No hacks. All it does is fire up another dnx instance which is exactly how it starts up if you run dnx-watch, dnx web or IISExpress.
|
0

Yes, you can.

Take a look at this issue https://github.com/aspnet/Mvc/issues/3410 and The mentioned package.

Basically you need to implement your own IApplicationEnvironment

Comments

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.