alenan2013 alenan2013 - 1 month ago 7
ASP.NET (C#) Question

Unit test method doesn't recognize app conf connection string

I have been working on ASP.NET MVC project Visual Studio 2012 Ultimate with Entity Framework. I have to include a Unit Test project into my solution. My problem is in that the test method (called Index()) can't recognize the application connection string in app.config. My Unit test method is:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ELSORegistry;
using ELSORegistry.DataAccess;
using ELSORegistry.Controllers;



namespace ELSORegistryUnitTests
{
[TestClass]
public class FirstControllerTest
{
[TestMethod]
public void Index()
{
//Arange
HomeController controller = new HomeController();

//Act
Guid? myGuid = new Guid("941b1615-f21b-4e2c-8fa8-0ed0d3f2de53");
ViewResult result = controller.Index(myGuid) as ViewResult;

//Assert
Assert.IsNotNull(result);
}
}
}


My Index() action in the Home Controller is:

using System;
using System.Diagnostics.Contracts;
using System.Web.Mvc;
using ELSORegistry.DataAccess;
using ELSORegistry.Models;
using Kendo.Mvc.UI;
using WebGrease.Css.Extensions;
using ELSORegistry.Extensions;



using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Linq;
using Kendo.Mvc.Extensions;
using System.Diagnostics;
using ELSORegistry.Helpers;
using Newtonsoft.Json;


namespace ELSORegistry.Controllers
{
[Authorize]
public class HomeController : Controller
{
[Authorize(Roles = "ECLS Center Data Manager, ECLS Center Administrator,ECLS Center Data Viewer, ECLS Center Data Entry")]
//[RequireHttps] // Enable for production
public ActionResult Index(Guid? CenterId)
{
//----------------------------------------
// Remove references to previous patients
//----------------------------------------
Session.Remove("Patient");
Session.Remove("PatientSummary");
Session.Remove("Run");
Session.Remove("RunDetail");
Session.Remove("Addendum");

// if user have this session then he will get edit forms. // Yes if Add new
Session.Remove("AddNewMode");
Session.Remove("AddNewRunId");
Center center;
if (CenterId == null)
{
center = Session["Center"] as Center;
Contract.Assert(center != null);
}
else
{ // set center by selected centerId from dropdownlist
center = new Repository().GetCenter(new Guid(CenterId.ToString()));
Session["Center"] = center;
center = Session["Center"] as Center;
Contract.Assert(center != null);
}


ViewBag.RunCounts = Session["RunCounts"];
ViewBag.ChartSummaries = Session["ChartSummaries"];



return View(new QuickAdd());

}


The bug is in the line:

center = new Repository().GetCenter(new Guid(CenterId.ToString()));


of the controller. The method GetCenter is:

public Center GetCenter(Guid centerId)
{
try
{
using (var context = new ELSORegistryEntities())
{
var center = context.Centers.FirstOrDefault(c => c.CenterId == centerId);
return center;
}
}
catch (InvalidOperationException ex)
{
throw new DataException(string.Format("Error retrieving center for CenterId {0}", centerId), ex);
}
}


and app.config is in the ELSORegistry.DataAccess project which is included in the test. The class ELSORegistryEntities() is:

namespace ELSORegistry.DataAccess
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;

public partial class ELSORegistryEntities : DbContext
{
public ELSORegistryEntities()
: base("name=ELSORegistryEntities")
{
}


The connection string is:

<connectionStrings>
<add name="ELSORegistryEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=NELAPC\NELA2014;initial catalog=ELSORegistry2;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>


The bug is : "Test method ELSORegistryUnitTests.FirstControllerTest.Index throw exception:System.Data.DataException:Error retrieving center for CenterId 941b1615-f21b-4e2c-8fa8-0ed0d3f2de53->System.InvalidOperationException: No connection string named'ELSORegistryEntities' could be found in the application config file". How can I solve this? Thank you in advance for any help.

Answer

You need to include an app.config file inside you unit test project. Only the running project's (in this case the unit test project) app.config is respected/used.

Comments