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

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

1 Answer 86 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steven T.
Top achievements
Rank 1
Steven T. asked on 28 Apr 2014, 07:07 PM
Given:

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!

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 30 Apr 2014, 02:59 PM
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:
[TestMethod]
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.

Regards,
Kaloyan
Telerik
 

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.

 
Tags
General Discussions
Asked by
Steven T.
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or