szp*_*pic 9 c# asp.net asp.net-mvc unit-testing
我有一个web项目的解决方案,我决定在解决方案中添加一个单元测试项目.在运行测试时,其中一个失败并出现此错误:
Result Message:
Test method DetailsRoleController threw exception:
System.InvalidOperationException: No connection string named 'EquipmentEntities' could be found in the application config file.
Run Code Online (Sandbox Code Playgroud)
这是我的测试脚本:
[TestClass]
public class RoleControllerTest
{
RoleController RC = new RoleController();
[TestMethod]
public void IndexRoleController()
{
}
[TestMethod]
public void DetailsRoleController()
{
var result = RC.Delete(1);
Assert.IsNotNull(result);
}
}
Run Code Online (Sandbox Code Playgroud)
和控制器方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
为什么这个测试失败了?
这个测试用例是不是使用主项目中的connectrionstrings/context运行的?
好的,我编辑了我的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>
Run Code Online (Sandbox Code Playgroud)
但现在我有另一个错误:
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.
Run Code Online (Sandbox Code Playgroud)
hut*_*oid 11
它不是默认情况下会从您的Web应用程序中提取它的情况.
它将在当前正在运行的项目中查找它.
您必须将其添加到包含连接设置的Unit Test项目中的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;
}
....
Run Code Online (Sandbox Code Playgroud)
然后你可以像下面那样模拟出来的存储库:
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
有关详情,请参阅此处:
http://kakimotonline.com/2011/02/13/tdd-in-asp-net-mvc-applications-with-moq-framework/
| 归档时间: |
|
| 查看次数: |
16145 次 |
| 最近记录: |