1

I have a problem that I haven't been able to find a solution to, and I wonder if anyone could give some advice.

I have a mocked datacontext/objectset, done through interfaces and t4 templates, with some ninject magic, with the intent of having in memory datasets for unit testing.

However, what should you do with the foreign key values/navigation properties?

Lets say I have hotels and customers, ctx.Hotels has some values, but Customer.Hotels does not. The get is something like this if it is a one-to-one relationship:

return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Hotel>("HotelModel.FK_Customers_Hotels", "Hotel").Value;

and one-to-many:

return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<BookingRow>("HotelModel.FK_BookingRows_Customers", "BookingRow");

My skill level just isn't enough to even understand what is going on here.

[edit:] Great Master Julie Lerman confirms that this is a dead end. You can't properly mock entityobjects, you need POCOs for that.

eatfrog
  • 95
  • 7

1 Answers1

3

Mocking ObjectContext when you are using EntityObject based entities is mostly impossible because for example RelationshipManager is a real class which cannot be replaced with your mock. Also your entities are heavily dependent on non mockable EF code.

Note: "Mostly" because you can mock it but you need special framework intercepting calls to real objects and forwarding them to your methods instead. That is possible only with TypeMock Isolator or MS Moles.

Btw. mocking EF code is something you don't want to do - go through this answer and linked answers. Some of them targets newer EF API but the problems are still same.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670