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

Mocking a DbSet don't work for find method

2 Answers 305 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Iman
Top achievements
Rank 2
Iman asked on 23 Aug 2013, 09:27 AM
Hi Telerik,
In JustMock documents (Entity Framework Mocking) you use :
IList<Dinner> FakeDinners()
And then :
Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());
to mock a DbSet. The problem is where I use " DbContext.DbSet<Dinner>.Find(2) " which gets from context or database the entity with primary key equal to 2. But in my test where I pass the FakeDinners() ,it doesn't work and return null. Looks like it doesn't know primary key in 
FakeDinners() as it is list. So what is solution?

2 Answers, 1 is accepted

Sort by
0
Accepted
Kaloyan
Telerik team
answered on 23 Aug 2013, 10:54 AM
Hi Iman,

In this case, you can directly arrange the behavior of the Find() method, like this:
[TestMethod]
public void ShouldReturnFakeCollectionWhenExpected()
{
    NerdDinners nerdDinners = new NerdDinners();
 
    // Arrange
    Mock.Arrange(() => nerdDinners.Dinners.Find(2)).Returns(FakeDinners().FirstOrDefault());
 
    // Act
    var query = nerdDinners.Dinners.Find(2);
 
    // Assert
    Assert.AreEqual(1, query.DinnerID);
}

I hope this helps. Let me know if I can assist you further.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Iman
Top achievements
Rank 2
answered on 23 Aug 2013, 11:31 AM
OK, I was thinking there is a better (smarter/automated) way to do this. anyway thanks for your replay.
Tags
General Discussions
Asked by
Iman
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Iman
Top achievements
Rank 2
Share this question
or