0

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 DbSets 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.

Kyle Vassella
  • 2,296
  • 10
  • 32
  • 62
  • I don't think doing a full copy of the dev database is a good idea for integration testing. What I would recommend using a special database with special data used in integration tests. Also you will probably need a data restoration strategy(when is database put to initial state) before each test, before every test run or periodically, since some tests might change the data that other tests depend on and you really can't be sure in what order the test will execute – Filip Cordas Oct 23 '18 at 22:34
  • Thanks. The `TransactionScope` code above is supposed to prevent the data from actually persisting in the test database - but I've also seen methods of creating/teardown of the database with each test (also closing any open connections on it), etc. That's what makes this so tricky - there's so many different ways to go about it and seemingly no tutorial that covers everything in a comprehensive way (since there are so many options). It's making me re-think integration testing. It seems like a good thing to have but time is also a factor. – Kyle Vassella Oct 23 '18 at 22:55
  • @FilipCordas I see - so just populate test database with only the data I need for testing. So how do I seed test database with the desired data from dev database? Is that what my `apiIntegrationTest` project's dbcontext subclass is for? Do I just add the dbset's of what I want? Even then - how do I pull this data from dev database? Do I just copy the desired dbsets from my main `api` project's dbcontext subclass (insightcodefirst.cs) ? – Kyle Vassella Oct 24 '18 at 17:01

0 Answers0