How to connect to Database from NUnit Test project?
<p>I am new to NUnit testing for asp.net mvc web app, project I want to test using entity frame work to connect with the database, from the test project I need to connect to the same data base of the project which I want to test .</p>

<p>controller which I am testing looks like as below.</p>

<pre><code>public class EXController : Controller
{
    private readonly AppDbContext _context;
    private readonly CommonDbContext _commonContext;

    public EXController(CommonDbContext commonContext, AppDbContext context)
    {
        _commonContext = commonContext;
        _context = context;
    }

     public ActionResult Index()
    {
    // some simple code.
    }
}
</code></pre>

<p>I am trying to write NUnit tests as below:
In references section of the test project I added Project under testing as dll, I am able to 
access all the classes from the project I want to test.</p>

<p>CommonDbContext and AppDbContext are the classes which I am using in the code under testing to connect with the data base, these classes takes argument as the connectionstring. </p>

<p>so I am passing the connection string to these classes to connect with the data base from Nunit test project.</p>

<p>I am trying to connect to the data base in NUnit test project, using following connection string.</p>

<pre><code>CommonDbContext objcdb = new CommonDbContext("metadata =\"res://*/Data.CommonDbModel.CommonDbModel.csdl|res://*/Data.CommonDbModel.CommonDbModel.ssdl|res://*/Data.CommonDbModel.CommonDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");

AppDbContext Objadb = new AppDbContext("metadata = \"res://*/Data.AppDbModel.AppDbModel.csdl|res://*/Data.AppDbModel.AppDbModel.ssdl|res://*/Data.AppDbModel.AppDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");

// creating instance for the contrller I want to test
ExController obController = new CLINController(objcdb, Objadb);
</code></pre>

<p>I am getting below exception when I am trying to connect with the database from NUnit test project.</p>

<pre><code>Exception:
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Format of the initialization string does not conform to specification starting at index 164.

**Stack Trace:**
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, String&amp; keyname, String&amp; keyvalue)
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at BidMaster.Web.Infrastructure.CurrentUser.get_User() in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Infrastructure\CurrentUser.cs:line 30
   at BidMaster.Web.Controllers.CLINController.Index(Nullable`1 id) in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Controllers\CLINController.cs:line 51
   at BidMaster.Test.Controllers.CLINControllerTests.IndexTest() in D:\BWM_sprint3_newcopy\BidMaster.Test\BidMaster.Test\Controllers\CLINControllerTests.cs:line 47
</code></pre>

<p>Any thing wrong with the connectin string which I am using? Please help me, please explain how to connect with the database from Unit test project. I am new to testing.</p>