Mocking a DbSet don't work for find method

3 posts, 1 answers
  1. Iman
    Iman avatar
    7 posts
    Member since:
    Jun 2013

    Posted 23 Aug 2013 Link to this post

    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. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 23 Aug 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Iman
    Iman avatar
    7 posts
    Member since:
    Jun 2013

    Posted 23 Aug 2013 Link to this post

    OK, I was thinking there is a better (smarter/automated) way to do this. anyway thanks for your replay.
Back to Top