Entity Model and ToList()

3 posts, 0 answers
  1. Jose
    Jose avatar
    5 posts
    Member since:
    Apr 2011

    Posted 11 May 2011 Link to this post

    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. Jose
    Jose avatar
    5 posts
    Member since:
    Apr 2011

    Posted 12 May 2011 Link to this post

    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 ?

  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 18 May 2011 Link to this post

    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
Back to Top