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:
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.