【问题标题】:Unit testing asp.net mvc4 with Entity Framework使用实体框架对 asp.net mvc4 进行单元测试
【发布时间】:2013-02-07 19:14:34
【问题描述】:

Web 请求“Url to Test”成功完成,但未运行测试。

这个错误可以通过不调用asp服务器来传递;注释掉 TestMethod 和 CreateTest 之间的行。但后来我收到以下错误?

System.ArgumentException:在配置中找不到指定的命名连接,不打算与 EntityClient 提供程序一起使用,或者无效。

测试项目中的 App 配置文件没有连接。我尝试插入相关的连接字符串和 我还将主项目中的 web.config 复制到项目中,但我仍然收到此错误?! 我研究了一些线程,这些线程表明我有一半使用某种假数据库?这是真的;如果可以的话有人可以指点我

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("C:\\Users\\Admin\\Documents\\Visual Studio 2010\\Projects\\CA3 EAD\\CA3 EAD", "/")]
    [UrlToTest("http://localhost:2124/")]
    public void CreateTest()
    {
        EmployeeController target = new EmployeeController(); // TODO: Initialize to an appropriate value
        Employee employee = new Employee(); // TODO: Initialize to an appropriate value
        //employee.Name = "test";
        //employee.Surname = "test";
        //employee.Town = "test"; ;
        //employee.County = "test";
        //employee.Country = "test";
        //employee.GradeID = 4;
        //ActionResult expected = target.Create(); // TODO: Initialize to an appropriate value
        //ActionResult actual = null;
        var expected = target.Create();
        var actual = target.Create(employee);
        //Assert.AreEqual(expected, actual);
        //Assert.Inconclusive("Verify the correctness of this test method.");
    }

【问题讨论】:

  • 您实际上是在执行集成测试,因为您要测试多个单元。如果你想要一个真正的控制器单元测试,你应该模拟任何数据访问。
  • 我之前在将 ef 附加到表单项目时遇到了问题,我通过将 app.config 复制到测试项目中解决了类似的错误。那么你是说 mvc4 不允许这种方法,在这种情况下我必须使用 mog 或 rhino?
  • 不,你不必嘲笑。就个人而言,除了使用单元测试进行模拟之外,我还会像您一样编写集成测试。也就是说,将 web.config 的相关部分复制到您的测试项目 app.config 应该解决您的问题。
  • 我暂时解决了我的单元测试问题。测试项目中的应用程序配置文件是裸露的,所以我从 web.config 复制了所有连接字符串;但是,如果我只复制我需要的那个,测试就可以了!

标签: c# visual-studio-2010 unit-testing asp.net-mvc-4 entity-framework-5


【解决方案1】:

这个问题困扰了我很久,也是一个颇有争议的问题。

Fake DbContext of Entity Framework 4.1 to Test

请参阅上述答案。

【讨论】:

    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多