5

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.

MBK
  • 525
  • 1
  • 8
  • 23
  • I agree with Gert Arnold response on a similar question [link] http://stackoverflow.com/questions/26894734/using-test-doubles-with-dbentityentry-and-dbpropertyentry. Mocking up a DBEntityEntry class is not a good idea and I should not try to do this in my unit tests. – MBK Oct 13 '15 at 02:32
  • You can do this: http://stackoverflow.com/questions/5035323/mocking-or-faking-dbentityentry-or-creating-a-new-dbentityentry – Yanet Silva Fernández Apr 07 '17 at 19:09

0 Answers0