1

I am developing an ASP.NET MVC application. The application runs perfectly. I can view, add, update and delete records, but when I run the unit tests, it shows an error on every test method.

The error is:

The underlying provider failed on Open.

Error details:

{"Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=21013; handshake=0; "}

What can be issue?

Unit Test Code

[TestMethod]
public void Test_Company_GetCompanyType()
{
    controllerBuilder.Session["loggedEmpId"] = 6;
    companyId = 66;

    //Case 1 : Does  Company have Type or not ?
    JsonResult actualCompanyType = oCompanyController.GetCompanyType(companyId) as JsonResult;          
    var result = actualCompanyType.Data;           
    Assert.IsNull(result,"Company have types");        
}
4

2 回答 2

1

这可能不是您实际问题的答案/解决方案,但总的来说是一个很好的提示。

我强烈建议不要在每个单元测试中都访问数据库,这同样适用于文件系统和其他慢速 IO 操作。
使用像存储库模式这样的简单模式来抽象出数据访问。然后,您可以实现一个假存储库(即由 a 支持List<T>)或使用moq 之类的模拟框架来设置一个模拟存储库。

通过这样做,您的测试将完全在内存中运行,因此运行速度更快。您也不需要关心连接字符串,并且您始终可以访问数据库。

于 2013-06-07T13:03:57.910 回答
-1

单元测试,使用不要打数据库。您使用 Moq 或 RhinoMocks 等模拟框架。如果您想访问数据库,这将调用集成测试。

集成测试非常耗时,因此其中的大量测试最终无法在自动化上通宵运行,但其中少数是好的。

根据我在上面看到的代码,如果您遇到问题,我将使用控制台应用程序或 linqpad 对此进行测试,否则 TestMethod 将及时进行故障排除。

于 2013-06-08T08:04:46.387 回答