I going to unit test a service which is using Entity Framework 6.
Sample scenario, it would have Blog and Post entity, Blog having zero or more Posts. In the service method it would return list of entities which having Blog's Title and Title of first Post of that Blog. It looks like below.
public class BlogService
{
private IBloggingContext _context;
public BlogService(IBloggingContext context)
{
_context = context;
}
public List<BlogPostSumarry> GeBlogSummary()
{
var query = from b in _context.Blogs
orderby b.Name
select new BlogPostSumarry
{
BlogTitle = b.Name,
PostTitle = b.Posts.FirstOrDefault().Title
};
return query.ToList();
}
}
}
Unit Test Method
[Test]
public void GeBlogSummary_WhenMatchFound()
{
var post = new List<Post>()
{
new Post() {PostId=45, Title="abc"}
};
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
}.AsQueryable();
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(c => c.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blogs = service.GeBlogSummary();
Assert.AreEqual(3, blogs.Count);
Assert.AreEqual("AAA", blogs[0].BlogTitle);
Assert.AreEqual("BBB", blogs[1].BlogTitle);
Assert.AreEqual("ZZZ", blogs[2].BlogTitle);
}
Unit test I use to mock DbContext and DbSet use in memory data. Problem is in case of blogs doesn’t have any post unit test will giving null reference exception where as in real scenario (Database) it works fine. This is because in a case of in memory it is Linq to Object where as in EF it is Linq to Entity.
For a example if I change the method like below it works UnitTest
public List<BlogPostSumarry> GeBlogSummary()
{
var query = from b in _context.Blogs
orderby b.Name
select new BlogPostSumarry
{
BlogTitle = b.Name,
//Manually checks for null validation works but any approach without change code for sack of unit test
PostTitle = b.Posts.Any() ? b.Posts.FirstOrDefault().Title : null
};
return query.ToList();
}
Once approach is change linq query for null check but I think it is not good approach. If so any suggestion how can I achieve my test?
b.Posts.FirstOrDefault()
to return anything?FirstOrDefault
then you have to be prepared for having to deal withnull
. Instead do something likePostTitle = b.Posts.FirstOrDefault()?.Title ?? "No posts found"
?.
operator is not supported in expression trees (yet), so your suggestion simply doesn't compile.null
if there are no posts.