Hi and thanks for the replies.
The suggestions with returning the result of a collection like
mock.SetupGet(c => c.list).Returns(new []{ new ItemType{ Price =
30 } });
looks interesting, I didn't know that was possible.
I am trying to test code that uses the entity framework.
There are several ways I guess, a test database or a in-memory fake
database (this is doable now with T4 POCOs).
But I really want to use Moq so I can define only the data needed for
that particular unit test rather than rolling up an entire database
for each test. I also find that when I do test against a fake database
my unit tests usually fail, not because of errors in the code, but
because of data missing from the database *sigh*. And it's obviously
very cumbersome to manually fill a 30 table database with test data
suited for various unit tests.
Okay anyway, one of the simpler methods I'd like to test was this one:
List<VehicleDto> IStorage.GetVehicles()
{
AssertPrivilege((int)Privileges.ReadVehicle);
var result = new List<VehicleDto>();
var vehicles = user.Customer.Vehicles.Where(v => v.Enabled);
vehicles.ToList().ForEach(v =>
result.Add(Mapper.MapVehicleDto(v)));
return result;
}
The "user" is part of the IEntityModelContainer and happens to be the
user who is logged into the system.
So I am breaking the law of demeter here, but for the entity framework
this is quite normal.
Now, I think you see the problem. I can do something like:
var mock = new Mock<IEntityModelContainer>();
var vehicles = new MockObjectSet<Vehicle>() { };
var customers = new MockObjectSet<Customer>() { };
mock.SetupGet(c => c.Customers).Returns(customers);
but it's tricky getting things connected the right way...