1

I have a ASP WEB API 2 app that uses Entity Framework 6 code first. I want to make selenium tests that uses a test database that is created by migrations.

I want to use a test db because the routes that I want to tests are listed in a file and might change if a entity is deleted (some routes looks like /entityName/id). In short, I need to delete and migrate a test db every time I run the tests.

The only part that I don't get is how to make the API use the test db for the duration of the test.

I don't know if it'll helps but im using dbcontext.

  • take a look here: http://stackoverflow.com/questions/23201150/how-to-switch-between-test-and-production-databases-with-entity-framework-5 or http://stackoverflow.com/questions/13857180/how-should-i-set-up-my-integration-tests-to-use-a-test-database-with-entity-fram. approach should be similar – vmg Jul 13 '16 at 19:46

1 Answers1

1

From:

https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.dbcontext(v=vs.113).aspx#M:System.Data.Entity.DbContext.#ctor(System.String)

DbContext Constructor (String) Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made. See the class remarks for how this is used to create a connection.

You can then create a connection string that connects to the test database during debug/testing and pass the name of that connection string when you create your DbContext for the tests.

If your tests are in a separate Tests project, you can crate a connection string in the App.config with the fully-qualified name of the DbContext child class from your main project and it will pick that up and use it as the connection string.

Josh K
  • 765
  • 3
  • 11