In my controller's unit tests, I mock up the db context class injecting a fake db context object into the controller constructor. The fake context class has the fake DBSet classes and they work fine. However, I don't know how to fake the DbEntityEntry.Entry() method for the POST unit test. The simplified POST method code is as follows
[ResponseType(typeof(Trip))]
public async Task<IHttpActionResult> PostTrip(Trip trip)
{
db.Trips.Add(trip);
await db.SaveChangesAsync();
//Load driver user object
db.Entry(trip).Reference(x => x.User).Load();
var tripDTO = new TripDTO()
{
Id = trip.Id,
Status = trip.Status,
BookedSeats = trip.BookedSeats,
DriverName = trip.User.Name,
};
return CreatedAtRoute("DefaultApi", new { id = trip.Id }, tripDTO);
}
Apparently, I have to mock up the DbEntityEntry.Entry() method in a way to make db.Entry(trip).Reference() method to work with LINQ expression. If you had a similar problem before, could you please help?
Thanks a lot.