I read so many (dozens of posts) about one thing:
Like unit test, business logic code that has Entity Framework code.
I have a WCF service with three layers:
- Service level
- Business logic level
- Data access level
My business logic uses DbContext for all database operations. All my objects are now POCOs (usually this is an ObjectContext, but I changed that).
I read here and here for reasons why we should not mock \ fake DbContext.
He said: "That's why I believe that code related to the / Linq -to-entity context should be covered by integration tests and work with a real database."
and: "Of course, your approach works in some cases, but a module testing strategy must work in all cases - for it to work, you must completely move EF and IQueryable from your test method."
My question is: how do you achieve this?
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.");
In the above code, there is a demonstration of the function from my business logic. The function has several "trips" to the database. I donāt understand how exactly I can remove the EF code from this function in a separate assembly so that I can unit test this function (by entering some fake data instead of the EF data) and integrate the test assembly containing the "EF functions".
Can Ladislav or anyone else help?
[change]
Here is another example code from my business logic, I donāt understand how I can āmove EF and IQueryable codeā from my test method:
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 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 name matches) if (String.IsNullOrEmusy(name) == false) users = users.Where(us => us.name == name); // Filter By Age (check if the user 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)); }