I am novice to Moq. I am unit testing an Entity Framework 6 project, following guidance provided here . So I have a templated method to create the fake tables:
protected Mock<DbSet<TheType>> MockDBSet<TheType>(List<TheType> data) where TheType : class
{
var mockSet = new Mock<DbSet<TheType>>();
var dataSet = data.AsQueryable();
mockSet.As<IQueryable<TheType>>().Setup(m => m.Provider).Returns(dataSet.Provider);
mockSet.As<IQueryable<TheType>>().Setup(m => m.Expression).Returns(dataSet.Expression);
mockSet.As<IQueryable<TheType>>().Setup(m => m.ElementType).Returns(dataSet.ElementType);
mockSet.As<IQueryable<TheType>>().Setup(m => m.GetEnumerator()).Returns(dataSet.GetEnumerator());
mockSet.Setup(x => x.Add(It.IsAny<TheType>()))
.Returns(new Func<TheType, TheType>(x =>
{
data.Add(x);
return data.Last();
}));
return mockSet;
}
Using the above works fine for adding and querying the fake database:
var db = new Mock<BloggingContext>();
db.Setup(m => m.Blog s).Returns(MockDBSet<Blog >(
new List<Blog>()
).Object);
BloggingContext context = db.Object;
Blog blog= new Blog();
context.Blogs.Add(blog); //fine
Assert.IsTrue(context.Blogs.Count() == 1); //fine
EF also provides the "Local" property on the entity collections which give access to un-saved entities. So, when not mocking, BloggingContext.Blogs.Local passes back a ObservableCollection collection of unsaved entities. Objects are moved from BloggingContext.Blogs.Local to BloggingContext.Blogs when the BloggingContext.SaveChanges() is called.
I wanted to mock this behavior, so I created a new class:
public class FakeBlogs : List<Blog>
{
ObservableCollection<Blog> _local = new ObservableCollection<Blog>();
ObservableCollection<Blog> Local { get { return _local; } }
public void Add (Blog item)
{
_local.Add(item);
}
}
When unit testing, the following code works:
var db = new Mock<BloggingContext>();
db.Setup(m => m.Blog s).Returns(MockDBSet<Blog>(
new FakeBlogs() //<===== Changed to use FakeBlogs
).Object);
BloggingContext context = db.Object;
Blog blog= new Blog();
context.Blogs.Add(blog); //fine
Assert.IsTrue(context.Blogs.Count() == 1); //fine
However, a using the Local property throws a NPE as the Local property is null.
var blog = (from i in context.Blogs.Local select i).FirstOrDefault();//throws NPE
How can I mock the Local property successfully?
Fixed. I added the following line to the MockDBSet template function:
mockSet.Setup(m => m.Local).Returns(data.Local);
And declared the "Local" property of FakeTable as public. Thanks to all who took an interest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.