Reputation: 3199
I am learning Microsofts built in Unit Testing capability in VS2010 and came across a problem.
[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void SomeTest()
{
//Arrange
HomeController controller = new HomeController();
//Act
ViewResult results = controller.Index() as ViewResult;
//Assert
ViewDataDictionary viewData = results.ViewData;
Assert.AreEqual(null, viewData["Message"]);
}
}
I know this will return failed, that isn't an issue. What is an issue however is that I am hitting my EntityFramework model "myModel.edmx" and getting the error "System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."
This is in the file MyModel.Designer.cs. The line in question is:
public Tool_Entities() : base("name=Tool_Entities", "Tool_Entities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
I know this line is ok as when I run the non-test project, I can connect to this model fine.
Upvotes: 2
Views: 683
Reputation: 191058
You should abstract out EF from your controllers in a service-oriented or repository way. That way you can remove the dependency (and inject a mock) for your unit tests and just test your controllers.
First create an interface. This is just a blueprint, you can make it what ever way you want.
public interface IToolRepository
{
void Add(Tool something);
IQueryable<Tool> Query { get; }
void Delete(Tool something);
}
Then implement this with EF.
Upvotes: 3