For reference, I've been using this thread for help.
I'm implementing NUnit
Integration Tests of our controller REST endpoints in a .NET Web API 2
project. We use an Entity Framework
code-first from database
approach to create our models and controllers. That's all we're using Entity
for - we don't have a separate Data Access Layer project nor a Migrations
folder, etc.
I've got my main api
project, and an apiIntegrationTests
project. My goal is to have my IntegrationTests project utilize a LocalDB database (populated w/ data from dev database), separate from our development database.
In the apiIntegrationTests
project, I've set a ConnectionString in App.config
for a LocalDb instance:
<add name ="TestDatabase" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Database=TestDatabase;Integrated Security=True"/>
I've created a base class which, hopefully, will run every time an NUnit
integration test is run. This likely has errors:
TestSetup.cs
using NUnit.Framework;
using System.Data.Entity;
using System.Transactions;
namespace api.IntegrationTests
{
public class TestSetup
{
// What is this?
protected MyDbContext DbContext;
protected TransactionScope TransactionScope;
// Maps to connection string in App.config
public const string TestDatabaseName = "TestDatabase";
[SetUp]
public void SetUp()
{
// Create the database if it doesn't exist
DbContext = new MyDbContext(TestDatabaseName);
DbContext.Database.CreateIfNotExists();
TransactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);
}
[TearDown]
public void TearDown()
{
if (TransactionScope != null)
TransactionScope.Dispose();
}
}
}
I'm running into trouble creating the MyDbContext
class that I call above. Intellisense throws an error on it since MyDbContext
has yet to be created. I don't know where to start or what MyDbContext
is supposed to do.
My goal with MyDbContext.cs
, I think, is to populate my Integration Test LocalDB instance with DbSet
s of my choosing derived from my original development database that I'm using in the main api
project. Correct or no?
Can anyone drop some knowledge on me? I'm unsure if populating my Integration Test db with all the data from dev db on each test is a good idea since it's a pretty large database. I'm hoping I can just specify some of the tables from dev db for now, then find a better solution for dev testing later (in-memory hosting?). Right now I'd just be happy if I could successfully create this LocalDb
test database and populate it with real data from dev db.