关于一件事,我已经阅读了很多(数十篇帖子):
如何对包含实体框架代码的业务逻辑代码进行单元测试。
我有一个 3 层的 WCF 服务:
- 服务层
- 业务逻辑层
- 数据访问层
我的业务逻辑将DbContext用于所有数据库操作。我所有的实体现在都是 POCO(以前是 ObjectContext,但我改变了它)。
我已经在这里和这里阅读了Ladislav Mrnka 的回答, 了解我们不应该模拟\ 伪造DbContext的原因。
他说: “这就是为什么我认为处理上下文/Linq-to-entities 的代码应该包含在集成测试中并针对真实数据库工作的原因。”
并且: “当然,您的方法在某些情况下有效,但单元测试策略必须在所有情况下都有效 - 要使其有效,您必须将 EF 和 IQueryable 完全从您的测试方法中移出。”
我的问题是 - 你是如何做到这一点的???
public class TaskManager
{
public void UpdateTaskStatus(
Guid loggedInUserId,
Guid clientId,
Guid taskId,
Guid chosenOptionId,
Boolean isTaskCompleted,
String notes,
Byte[] rowVersion
)
{
using (TransactionScope ts = new TransactionScope())
{
using (CloseDBEntities entities = new CloseDBEntities())
{
User currentUser = entities.Users.SingleOrDefault(us => us.Id == loggedInUserId);
if (currentUser == null)
throw new Exception("Logged user does not exist in the system.");
// Locate the task that is attached to this client
ClientTaskStatus taskStatus = entities.ClientTaskStatuses.SingleOrDefault(p => p.TaskId == taskId && p.Visit.ClientId == clientId);
if (taskStatus == null)
throw new Exception("Could not find this task for the client in the database.");
if (taskStatus.Visit.CustomerRepId.HasValue == false)
throw new Exception("No customer rep is assigned to the client yet.");
TaskOption option = entities.TaskOptions.SingleOrDefault(op => op.Id == optionId);
if (option == null)
throw new Exception("The chosen option was not found in the database.");
if (taskStatus.RowVersion != rowVersion)
throw new Exception("The task was updated by someone else. Please refresh the information and try again.");
taskStatus.ChosenOptionId = optionId;
taskStatus.IsCompleted = isTaskCompleted;
taskStatus.Notes = notes;
// Save changes to database
entities.SaveChanges();
}
// Complete the transaction scope
ts.Complete();
}
}
}
在附加的代码中,有一个来自我的业务逻辑的函数的演示。该函数有几次到数据库的“旅行”。我不明白如何将 EF 代码从这个函数剥离到一个单独的程序集中,以便我能够对这个函数进行单元测试(通过注入一些假数据而不是 EF 数据),并集成测试程序集包含“EF 函数”。
拉迪斯拉夫或其他人可以帮忙吗?
[编辑]
这是我的业务逻辑中的另一个代码示例,我不明白如何从我的测试方法中“移动 EF 和 IQueryable 代码”:
public List<UserDto> GetUsersByFilters(
String ssn,
List<Guid> orderIds,
List<MaritalStatusEnum> maritalStatuses,
String name,
int age
)
{
using (MyProjEntities entities = new MyProjEntities())
{
IQueryable<User> users = entities.Users;
// Filter By SSN (check if the user's ssn matches)
if (String.IsNullOrEmusy(ssn) == false)
users = users.Where(us => us.SSN == ssn);
// Filter By Orders (check fi the user has all the orders in the list)
if (orderIds != null)
users = users.Where(us => UserContainsAllOrders(us, orderIds));
// Filter By Marital Status (check if the user has a marital status that is in the filter list)
if (maritalStatuses != null)
users = users.Where(pt => maritalStatuses.Contains((MaritalStatusEnum)us.MaritalStatus));
// Filter By Name (check if the user's name matches)
if (String.IsNullOrEmusy(name) == false)
users = users.Where(us => us.name == name);
// Filter By Age (check if the user's age matches)
if (age > 0)
users = users.Where(us => us.Age == age);
return users.ToList();
}
}
private Boolean UserContainsAllOrders(User user, List<Guid> orderIds)
{
return orderIds.All(orderId => user.Orders.Any(order => order.Id == orderId));
}