我有一个web项目的解决方案,我决定在解决方案中添加一个单元测试项目。在运行测试时,其中一个失败并出现此错误:
Result Message:
Test method DetailsRoleController threw exception:
System.InvalidOperationException: No connection string named 'EquipmentEntities' could be found in the application config file.
这是我的测试脚本:
[TestClass]
public class RoleControllerTest
{
RoleController RC = new RoleController();
[TestMethod]
public void IndexRoleController()
{
}
[TestMethod]
public void DetailsRoleController()
{
var result = RC.Delete(1);
Assert.IsNotNull(result);
}
}
和控制器方法:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Role role = db.Roles.Find(id);
if (role == null)
{
return HttpNotFound();
}
return View(role);
}
为什么这个测试失败了?
这个测试用例是否使用主项目中的连接字符串/上下文运行?
好的,我编辑了我的appconfig,现在正在查看:
<configuration>
<appSettings>
</appSettings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="EquipmentEntities" connectionString="metadata=res://*/Models.MagazynModel.csdl|res://*/Models.MagazynModel.ssdl|res://*/Models.MagazynModel.msl;provider=System.Data.SqlClient;provider connection string="data source=XYZ\sqlexpress;initial catalog=Equipment;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
但现在又出现了另一个错误:
Result Message: Unable to create instance of class magazynTest.Controllers.RoleControllerTest. Error: System.TypeInitializationException:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException:
Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section entityFramework.
答案 0 :(得分:10)
默认情况下,它不会从您的Web应用程序中提取它。
它将在当前正在运行的项目中查找它。
您必须将其添加到包含连接设置的单元测试项目中的app.config文件中。
另一种方法是将它注入到你的控制器中,这样你在测试中可以传入一个模拟对象,这样就可以防止必须处理实际的数据库实例。
如果您重新设计了使用存储库模式的方法,而不会看到类似下面的类:
public class RoleController : Controller
{
private IRoleRepository roleRepository;
// This constructor would not be needed if you were to use IOC container
public RoleController ()
{
this.roleRepository = new RoleRepository(new RoleContext());
}
public RoleController(IRoleRepository studentRepository)
{
this.roleRepository = roleRepository;
}
....
然后你可以像下面那样嘲笑存储库:
[TestClass]
public class RoleControllerTest
{
private Mock<IRoleRepository> _roleRepository;
[SetUp]
public void SetUp()
{
_roleRepository = new Mock<IRoleRepository>();
}
[TestMethod]
public void IndexRoleController()
{
}
[TestMethod]
public void DetailsRoleController()
{
RoleController RC = new RoleController(_roleRepository.Object);
var result = RC.Delete(1);
Assert.IsNotNull(result);
}
}
有关详细信息,请参阅此处:
http://kakimotonline.com/2011/02/13/tdd-in-asp-net-mvc-applications-with-moq-framework/
答案 1 :(得分:6)
将ConnectionStrings部分从webConfig
复制到测试项目appConfig
答案 2 :(得分:0)
别忘了在测试项目中安装实体框架