How to mock ObjectResult<T> generated from Entity Framework 6

2 posts, 0 answers
  1. Steven T.
    Steven T. avatar
    4 posts
    Member since:
    Jul 2013

    Posted 28 Apr 2014 Link to this post


    MyDbContext.GetProjects() which is generated via EntityFramework 6 from a stored procedure.

    The return type is ObjectResult<MyProject>

    I want to mock the results with a specific collection of Data.

    Mock.Arrange(() => MyDbContextMock.GetProjects()).Returns(FakeEmptyList());

    trying to use the following:

    public ObjectResult<MyProject> FakeEmptyList()
      return new ObjectResult<MyProject>();

    results in:

    Error 18 The type 'System.Data.Entity.Core.Objects.ObjectResult<T>' has no constructors defined ............

    I am new to JustMock and appreciate the help.  Thanks!
  2. Kaloyan
    Kaloyan avatar
    878 posts

    Posted 30 Apr 2014 Link to this post

    Hello Steven,

    To fake an ObjectResult<T> you will need to use the ReturnsCollection method of JustMock. Let's assume the following code:
    public class NerdDinners
        public ObjectResult<Dinner> Dinners { get; set; }

    Now, to fake the return value of the above property we can use a simple list, like this:
    public IList<Dinner> FakeDinners()
        return new List<Dinner>()
            new Dinner()
                DinnerID = 1,
                Title = "FakeDinner 1",
                EventDate = new DateTime(1222, 12, 12),
                Address = "Fake Address 1",
                HostedBy = "Fake Host 1"
            new Dinner()
                DinnerID = 2,
                Title = "FakeDinner 2",
                EventDate = new DateTime(1222, 12, 12),
                Address = "Fake Address 2",
                HostedBy = "Fake Host 2"
            new Dinner()
                DinnerID = 3,
                Title = "FakeDinner 3",
                EventDate = new DateTime(1222, 12, 12),
                Address = "Fake Address 3",
                HostedBy = "Fake Host 3"

    And the actual test is as follows:
    public void ShouldReturnFakeCollectionWhenExpected()
        // ARRANGE
        NerdDinners nerdDinners = new NerdDinners();
        Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());
        // ACT
        var actualQuery = nerdDinners.Dinners;
        // ASSERT
        Assert.AreEqual(3, actualQuery.Count());

    Please, note the usage of the ReturnsCollection method. It dynamically converts the List of Dinners and allows you to arrange it as a return value of an ObjectResult type. More about this can be found here and here.

    I hope this helps. Please, let me know if there is anything else I can help you with.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top