2

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.

2
  • Take a look at this article: bit.ly/bF7jL3. It explains how to build an (LINQ enabled) abstraction around your O/RM. Commented Sep 12, 2011 at 13:27
  • Are your tests in a separate project? (in that case the tests run off their own app.config) Commented Sep 15, 2011 at 21:29

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How would I go about that? I am fairly new to the EntityFramework. I'd guess that I would have to create an interface somehow for the EF model in question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.