我正在使用Microsoft Fakes代码伪造DBcontext中的实体。我正面临一个问题。 Fake DBSet上的AsNoTracking()方法返回null。
例如:当Employee实体伪造时,DBContext.Employee.AsNoTracking()返回null。
解决此问题的任何解决方案都非常有用
提前致谢
答案 0 :(得分:0)
这是代码。GetEmployees()
返回异常“Value is null”。 AsNoTracking()
方法中发生错误。但是如果我删除AsNoTracking()
,测试方法就可以了。
[TestMethod]
public void TestGetEmployeesList()
{
using (ShimsContext.Create())
{
ShimDbSet<Employee> EmployeeSet = new ShimDbSet<Employee>();
ShimEntities dbShim = new ShimEntities();
List<Employee> tempEmployeeList = GetEmployeeList();
EmployeeSet.Bind(tempEmployeeList.AsQueryable());
dbShim.EmployeeGet = () => { return EmployeeSet.Instance; };
Employee employeeTest = new Employee() { DB = databaseShim.Instance };
List<EmployeeTransfer> employeeList = employeeTest.GetEmployees("TestEmployee");
Assert.AreEqual(2, employeeList.Count);
}
}
public static List<Employee> GetEmployeeList()
{
List<Employee> employeeList = new List<Employee>
{
new Employee
{
EmployeeId = 1,
EmployeeName ="TestEmployee",
},
new Employee
{
EmployeeId = 2,
EmployeeName ="TestEmp",
}
};
return EmployeeList;
}
public EmployeeData GetEmployees()
{
var query = from item in DB.Employee.AsNoTracking()
select item;
var employeeList = query.ToList();
return employeeList.FirstOrDefault();
}