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

EntityFramework mocking: ObjectContext.Dispose() throwing FileLoadException.

1 Answer 25 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
César
Top achievements
Rank 1
César asked on 07 Nov 2012, 10:12 PM
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.

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 09 Nov 2012, 09:01 PM
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.

Tags
General Discussions
Asked by
César
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or