我此刻正在吃东西.就像Entity Framework不可测试一样.我已经阅读了很多帖子和帖子,他们使用工作单元或moq或repo模式.
我处于一个阶段,我不能改变我的应用程序的架构.该应用程序在此时完全正常工作,但为了确保我需要具有高代码覆盖率,因此测试它是.
为了测试,我使用'伪上下文'方法,我可以使用虚假的方法进行模拟,使用真实方法连接数据库.
我用这个作为例子. http://romiller.com/2010/09/07/ef-ctp4-tips-tricks-testing-with-fake-dbcontext/
在那里,您可以看到上下文被拆分并用作接口.喜欢:
public interface IEmployeeContext
{
IDbSet Department Departments { get; }
IDbSet Employee Employees { get; }
int SaveChanges();
}
public class EmployeeContext : DbContext, IEmployeeContext
{
public IDbSet Department Departments { get; set; }
public IDbSet Employee Employees { get; set; }
}
public class FakeEmployeeContext : IEmployeeContext
{
public FakeEmployeeContext()
{
this.Departments = new FakeDepartmentSet();
this.Employees = new FakeEmployeeSet();
}
public IDbSet Department Departments { get; private set; }
public IDbSet Employee …Run Code Online (Sandbox Code Playgroud)