EntityFramework mocking: ObjectContext.Dispose() throwing FileLoadException.

2 posts, 0 answers
  1. César
    César avatar
    1 posts
    Member since:
    Oct 2012

    Posted 07 Nov 2012 Link to this post

    Hi.

    I recently downloaded a sample project from this thread and it worked fine.

    So, I started to make some modifications, adding this simple test method to the JustMockTest1 class:
    [TestMethod]
    public void ShouldAssertMockedGetAllCategories()
    {
        NorthwindEntities entities = Mock.Create<NorthwindEntities>();
         
        Category category = new Category()
        {
            CategoryName = "Beer"
        };
        List<Category> allCategories = new List<Category>
        {
            category
        };       
         
        // Arrange           
        Mock.Arrange(() => entities.Categories).ReturnsCollection(allCategories);
     
        // Act
        List<Category> categoriesList= entities.Categories.ToList();
         
        // Assert
        Assert.AreEqual(1, categoriesList.Count);
        Assert.AreSame(category, categoriesList[0]);
    }

    Again, everything worked like a charm.

    Since I don't access a ObjectContext directly I tried to make things a little bit real world like.

    [TestMethod]
    public void ShouldAssertMockedGetAllCategories()
    {
        NorthwindEntities entities = Mock.Create<NorthwindEntities>();
         
        Category category = new Category()
        {
            CategoryName = "Beer"
        };
        List<Category> allCategories = new List<Category>
        {
            category
        };       
         
        // Arrange           
        Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
     
        // Act
        //Some instance of some class calls the following lines of code somewhere, somehow. =)
        NorthwindEntities ne = new NorthwindEntities();
        List<Category> categoriesList = ne.Categories.ToList();
         
        // Assert
        Assert.AreEqual(1, categoriesList.Count);
        Assert.AreSame(category, categoriesList[0]);
    }

     

    Test passed. \o/

    Oops! I forget to call Dispose(). Let's first try the default way to do that.

    [TestMethod]
    public void ShouldAssertMockedGetAllCategories()
    {
        NorthwindEntities entities = Mock.Create<NorthwindEntities>();
     
        Category category = new Category()
        {
            CategoryName = "Beer"
        };
        List<Category> allCategories = new List<Category>
        {
            category
        };
     
        // Arrange           
        Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
     
        // Act
        //Some instance of some class calls the following lines of code somewhere, somehow. =)
        List<Category> categoriesList;
        using (NorthwindEntities ne = new NorthwindEntities())
        {        
            categoriesList = ne.Categories.ToList();
        }
     
        // Assert
        Assert.AreEqual(1, categoriesList.Count);
        Assert.AreSame(category, categoriesList[0]);
    }

    Uh-oh. An Exception was thrown at the end of using block.

    Even if I call Dispose() directly (instead of using the using statement), test fails for the same reason.

    [TestMethod]
    public void ShouldAssertMockedGetAllCategories()
    {
        NorthwindEntities entities = Mock.Create<NorthwindEntities>();
     
        Category category = new Category()
        {
            CategoryName = "Beer"
        };
        List<Category> allCategories = new List<Category>
        {
            category
        };
     
        // Arrange           
        Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
     
        // Act
        //Some instance of some class calls the following lines of code somewhere, somehow. =)
        NorthwindEntities ne = new NorthwindEntities();
        List<Category> categoriesList = ne.Categories.ToList();
        ne.Dispose();
     
        // Assert
        Assert.AreEqual(1, categoriesList.Count);
        Assert.AreSame(category, categoriesList[0]);
    }

    Am I missing something here?

    Thanks in advance.

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 09 Nov 2012 Link to this post

    Hi Cesar,

    Thanks again for reporting the issue. 

    You absolutely got it right that JustMock is throwing exception on Dispose () call. We however, recently fixed it and it will be available in upcoming SP1 early next month. However, if you need the fix urgently then I would request you to create a support ticket where I will send you the updated build.
      

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top