This is a migrated thread and some comments may be shown as answers.

Entity Model and ToList()

2 Answers 105 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jose
Top achievements
Rank 1
Jose asked on 12 May 2011, 01:18 AM
I am running into some exceptions when I try and mock some property calls from an entity framework model.

var fakeEntities = Mock.Create<SomeEntities>(Constructor.Mocked);
  
Mock.Arrange(() => fakeEntities.SomeObjectSet.Count()).Returns(5);
Mock.Arrange(() => fakeEntities.SomeObjectSet.ToList()).Returns(someList);

The first arrange works fine, but the ToList() call results in an ArgumentNullException.

It looks like it is actually trying to convert 'SomeObject' to a list instead of getting mocked. Any Ideas?

2 Answers, 1 is accepted

Sort by
0
Jose
Top achievements
Rank 1
answered on 12 May 2011, 06:51 PM
Some updates on my findings.

Here is the code (C#) I was unsuccessfully able to run.

var fakeEntities = Mock.Create<SomeEntities>(Constructor.Mocked);
   
Mock.Arrange(() => fakeEntities.SomeObjectSet.Count()).Returns(5);
Mock.Arrange(() => fakeEntities.SomeObjectSet.ToList()).Returns(someList);
 
int count = fakeEntities.SomeObjectSet.Count();
var list = fakeEntities.SomeObjectSet.ToList();

The call to fakeEntities.Count() worked as expected, but an exception was thrown on the fakeEntities.ToList() call. I rearranged the code as follows and was able to get it to run:

using Telerik.JustMock.Helpers;
 
var fakeEntities = Mock.Create<SomeEntities>(Constructor.Mocked);
    
Mock.Arrange(() => fakeEntities.SomeObjectSet.Count()).Returns(5);
Mock.Arrange(() => fakeEntities.SomeObjectSet).ReturnsCollection(someList);
  
int count = fakeEntities.SomeObjectSet.Count();
var list = fakeEntities.SomeObjectSet.ToList();

There are a few interesting things to note here. I still have to mock the .Count() call or an exception is thrown, but I do not have to mock the .ToList() call.

Second, The order of the Mock.Arrange() calls seems to be important. The order it is set in the above code, is the order it must be in, otherwise the ToList() call will fail. In other words, if it is set like this instead:

Mock.Arrange(() => fakeEntities.SomeObjectSet).ReturnsCollection(someList);
Mock.Arrange(() => fakeEntities.SomeObjectSet.Count()).Returns(5);

The count call still works correctly, but an exception is thrown when I call .ToList().

Can anyone clarify what or why this is happening ?

0
Ricky
Telerik team
answered on 18 May 2011, 04:56 PM
Hi Jose,
Thank you for taking time in sending the issue.

However, ReturnsCollection actually sets your target DataContext that implements IEnumerable or IQueryable to your fake collection. Here, Count() and ToList() both are going to execute the Count() and ToList() from your fake collection respectively. That is the reason, you don't explicitly has to mock ToList() once you do ReturnsCollection.

In case of throwing null for Count() though fakeEntities.SomeObjectSet is pointing to your fake collection, we recently fixed this issue and if you could open a support ticket (since this is a public forum post) then we can attach the latest internal build for you.


Kind regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Jose
Top achievements
Rank 1
Answers by
Jose
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or