Doug Chamberlain
Doug Chamberlain

Reputation: 11341

How do i create a unit test for a dbcontext when the context is initialized in my web.config file?

my project in the web.config file

...    
<add name="fooDBContext"
        connectionString="Data Source=foo;Initial Catalog=WebData;User Id=scpauser;Password=password;" providerName="System.Data.SqlClient" />
    </connectionStrings>

My unit test which is in a separate project

namespace unit_tests.ModelTests
{
    [TestClass]
    public class CAMADBContextTest
    {
        [TestMethod]
        public void TestMethod1()
        {
            Database.SetInitializer<mvc3test.Models.CAMADBContext>(null);

            var db = new mvc3test.Models.CAMADBContext();
            var dps = db.DataProperty.Where(p => p.AccountNumber == 141);

        }
    }
}

Upvotes: 1

Views: 239

Answers (1)

kaps
kaps

Reputation: 478

For similar situation in my project I added config entries from web.config in web application to App.config in unit test project. Then I didn't need to do any change in code to read config entry and it worked same as in web application.

Upvotes: 1

Related Questions