WebAPI Moq Async EF6 UnitTesting - entity-framework

I'm using Moq to make Mocks for the EF6 Async methods. But something is not working right because i get the following exception:
The source IQueryable doesn't implement IDbAsyncEnumerable{0}. Only
sources that implement IDbAsyncEnumerable can be used for Entity
Framework asynchronous operations. For more details see
http://go.microsoft.com/fwlink/?LinkId=287068.
I have the following code:
UnitTest1.cs
var mockSet = new Mock<DbSet<MyModel>>();
mockSet.As<IDbAsyncEnumerable<MyModel>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<MyModel>(data.GetEnumerator()));
mockSet.As<IQueryable<MyModel>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<MyModel>(data.Provider));
mockSet.As<IQueryable<MyModel>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<MyModel>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<MyModel>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<PDBContext>();
mockContext.Setup(c => c.GetSet<MyModel>()).Returns(mockSet.Object);
//Test!
var controller = new MyModelController();
controller.MyModelStore = new MyModelStore(mockContext.Object);
var result = await controller.GetBestOppertunity(2) as OkNegotiatedContentResult<List<MyModel>>;
//Result
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Content.Count);
MyModelStore.cs
public async Task<List<MyModel>> GetBestOppertunity(int limit)
{
return await Context.GetSet<MyModel>().OrderByDescending(x => x.StartDate).Take(limit).ToListAsync();
}
I based the code on the following blog to prevent the error from happening: http://msdn.microsoft.com/en-us/data/dn314429
But exactly the error happends that i'm tryging to prevent. The error is explained here: http://msdn.microsoft.com/en-US/data/dn313107
The Error happends in the code: GetBestOppertunity(int limit) in MyModelStore.cs.
The GetSet Method showed there is nothing more than a wrapper to be able to attach unit testing to the Set method.
PDBContext.cs (DatabaseFirst)
public partial class PDBContext : IPDBContext
{
public virtual IDbSet<T> GetSet<T>() where T : class
{
return Set<T>();
}
}
Any Ideas what i'm doing wrong?

Related

Cannot resolve parameter 'MediatR.ServiceFactory serviceFactory' (MediatR with Autofac)

I'm attempting to run MediatR with Autofac in .NET Framework 4.6.2.
My registration:
public class MediatorModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof(IMediator ).Assembly).AsImplementedInterfaces();
builder.RegisterType<HealthCheckQueryHandler>().As<IRequestHandler<HealthCheckQuery, HttpStatusCode>>();
}
}
My controller:
[RoutePrefix("api/v1/healthcheck")]
public class HealthCheckController : ApiController
{
private readonly IMediator _mediator;
public HealthCheckController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet]
[Route("")]
public async Task<HttpStatusCode> Get()
{
var query = new HealthCheckQuery();
var result = await _mediator.Send(query);
return result;
}
}
When I try to hit this api method, I receive the following error:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MediatR.Mediator' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'MediatR.ServiceFactory serviceFactory' of constructor 'Void .ctor(MediatR.ServiceFactory)
The wiki (https://github.com/jbogard/MediatR/wiki) asks us to register SingleInstanceFactory and MultiInstanceFactory. But Visual Studio is not able to resolve either of these symbols.
How can I resolve this to get MediatR working with Autofac?
I know this is an old post, but in case anyone else googles for this.
You're missing registering the ServiceFactory instance.
builder.Register<ServiceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
Taken from here: MediatR Examples
I assume you are using Mediatr v3.0.x.
You are missing the registrations for the SingleInstanceFactory and MultiInstanceFactory as here below.
builder.Register<SingleInstanceFactory>(ctx => {
IComponentContext c = ctx.Resolve<IComponentContext>();
return type => {
Object o;
return c.TryResolve(type, out o) ? o : null;
};
});
builder.Register<MultiInstanceFactory>(ctx => {
IComponentContext c = ctx.Resolve<IComponentContext>();
return type => (IEnumerable<Object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(type));
});

How to log ASP.NET Core input JSON serialization errors

We have ASP.NET Core application which we use as a REST api. Methods use [FromBody]MyClass param as input. Problem is if the client sends an invalid JSON string the input value becomes null due to JSON serialization errors. Is there a way to log such errors?
Edit: I'm looking for application-wide solution not per-method..
I've solved this with adding an error handler in Startup.cs:
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) =>
{
//Log args.ErrorContext.Error details...
};
});
You can use an action filter to capture any model binding error (including deserialization errors). You can pull the error from the ActionExecutingContext.
EDIT: To use this approach, You need to suppress the model state invalid filter because by default web api will automatically return a 400 if there is a binding error. https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.apibehavioroptions.suppressmodelstateinvalidfilter#microsoft-aspnetcore-mvc-apibehavioroptions-suppressmodelstateinvalidfilter
public class LogModelBindingErrorFilter : IActionFilter
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
var messages = context.ModelState.Values
.SelectMany(x => x.Errors)
.Where(x => x.Exception is JsonException)
.Select(x => x.ErrorMessage);
_logger.Log(LogLevel.Error, string.Join(", ", messages));
}
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}

Create unit test for View() with a List in ASP.net core MVC application

I am new in unit testing. I have a controller - StudentsController with dependency injection and there my Index() method:
public class StudentsController : Controller
{
public readonly UniversityContext _context;//Database
public StudentsController(UniversityContext context)
{
_context = context;
}//Constructor with database
// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context.Students.ToListAsync());
}
}
Next i need to write a correct unit test code, that check, if:
1) a View() have a list with my students
2) The query with students is not null.
I read about Mock objects, but I don't know how to write the correct code. My code that I wrote so far:
public class StudentsControllerTests
{
[Fact]
public async Task Index_ReturnsAViewResult_WithAListOfStudents()
{
var mockRepo = new Mock<UniversityContext>();
mockRepo.Setup(repo => repo.Students.ToList()).Returns(GetTestStudents());//There i get following error:Expression references a method that does not belong to the mocked object
var controller = new StudentsController(mockRepo.Object);
// Act
var result = controller.Index();
//// Assert
var viewResult = Assert.IsType<ViewResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<Student>>(
viewResult.ViewData.Model);
Assert.NotNull(model);//Second Condition
}
public List<Student> GetTestStudents()
{
var sessions = new List<Student>();
sessions.Add(new Student()
{
bDate = new DateTime(1994, 7, 2),
Name = "Test One"
});
sessions.Add(new Student()
{
bDate = new DateTime(1995, 7, 1),
Name = "Test Two"
});
return sessions;
}
}
Can someone explain me, how to correct my code?
You only need to mock the members of the context, which in this case is the .Students property. ToList is an extension method being call on the property and cannot be mocked by moq.
Also .Students is a DbSet and would need to be mocked as well.
Using the test classes from this answer :
How to mock an async repository with Entity Framework Core
The following generic extension methods were derived
public static class MockDbSetExtensions {
public static Mock<DbSet<T>> AsDbSetMock<T>(this IEnumerable<T> list) where T : class {
IQueryable<T> queryableList = list.AsQueryable();
Mock<DbSet<T>> dbSetMock = new Mock<DbSet<T>>();
dbSetMock.As<IQueryable<T>>().Setup(x => x.Provider).Returns(queryableList.Provider);
dbSetMock.As<IQueryable<T>>().Setup(x => x.Expression).Returns(queryableList.Expression);
dbSetMock.As<IQueryable<T>>().Setup(x => x.ElementType).Returns(queryableList.ElementType);
dbSetMock.As<IQueryable<T>>().Setup(x => x.GetEnumerator()).Returns(() => queryableList.GetEnumerator());
return dbSetMock;
}
public static Mock<DbSet<T>> ToAsyncDbSetMock<T>(this IEnumerable<T> source)
where T : class {
var data = source.AsQueryable();
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IAsyncEnumerable<T>>()
.Setup(m => m.GetEnumerator())
.Returns(new TestAsyncEnumerator<T>(data.GetEnumerator()));
mockSet.As<IQueryable<T>>()
.Setup(m => m.Provider)
.Returns(new TestAsyncQueryProvider<T>(data.Provider));
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
return mockSet;
}
}
With the above utilities, update
mockRepo.Setup(repo => repo.Students.ToList()).Returns(GetTestStudents());
To
var studentsMockedDbSet = GetTestStudents().ToAsyncDbSetMock();
mockRepo.Setup(repo => repo.Students).Returns(studentsMockedDbSet.Object);

Entity Framework 6 Generic Eager Loading Query Method

I am writing a generic querying method in Entity Framework 6, based off of this helpful article. Here's how it looks:
public static T QueryEagerLoad<T>(Expression<Func<T, bool>> match) where T : class
{
using (var databaseContext = new ClearspanDatabaseContext())
{
databaseContext.Configuration.LazyLoadingEnabled = false;
T retrievedObject = databaseContext.Set<T>().SingleOrDefault(match);
return retrievedObject;
}
}
I'm attempting to eagerly load any related entities, so I include disable to configuration variable LazyLoadingEnabled. While it loads the object, it does not load the related entities, per my view in the debugger. Why would this be? Am I missing something? I should note that I'm using Npgsql. Thanks in advance.
See Mikael Östberg's answer to this question. To use a generic method for querying with eager loading, it seems necessary to inject the includes. Here's how the generic method shaped up:
public static T Query<T>(Expression<Func<T, bool>> match, List<Expression<Func<T, object>>> includes) where T : class
{
using (var databaseContext = new ClearspanDatabaseContext())
{
var dataSet = databaseContext.Set<T>(); // Get the relevant DataSet
T retrievedObject = includes.Aggregate( // Eagerly load the passed navigation properties
dataSet.AsQueryable(),
(current, include) => current.Include(include)
).SingleOrDefault(match); // Find exactly one or zero matches
return retrievedObject;
}
}
And an example of a call that injects the properties to eagerly load (the includes parameter in the generic method above):
public static Lumber GetLumber(int databaseId)
{
Expression<Func<Lumber, object>> lengthProperty = (lumber => lumber.Length);
Expression<Func<Lumber, object>> thicknessProperty = (lumber => lumber.Thickness);
Expression<Func<Lumber, object>> widthProperty = (lumber => lumber.Width);
List<Expression<Func<Lumber, object>>> lumberNaviationProperties = new List<Expression<Func<Lumber, object>>>() { lengthProperty, thicknessProperty, widthProperty };
Lumber retrievedLumber = DatabaseOperations.Query<Lumber>((lumber => lumber.DatabaseId == databaseId), lumberNaviationProperties);
return retrievedLumber;
}

How do you mock adding items to a repository or DbContext using moq?

The examples I've seen for using moq for a repository only show how to mock things being returned. I have a somewhat strange requirement: when a query is executed, if a condition exists, a certain item should be added to the repository. I am wondering how to test this without querying the database. I know how to mock the condition existing, but then how do you setup the mock so that you can test that the certain item is added?
Try to use fake in memory repository instead of moq, for example universal generic repository for all entities:
public interface IInMemoryRepository<T> where T : class
{
IQueryable<T> GetAll();
void Create(T item);
void Update(T item);
T GetItem(Expression<Func<T, bool>> expression);
void Delete(T item);
}
public class InMemoryRepository<T> : IInMemoryRepository<T> where T : class
{
private int _incrementer = 0;
public Dictionary<int, T> List = new Dictionary<int, T>();
public IQueryable<T> GetAll()
{
return List.Select(x => x.Value).AsQueryable();
}
public void Create(T item)
{
_incrementer++;
item.GetType().GetProperties().First(p => p.Name == "Id").SetValue(item, _incrementer, null);
List.Add(_incrementer, item);
}
public void Update(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List[key] = item;
}
public T GetItem(Expression<Func<T, bool>> expression)
{
return List.Select(x => x.Value).SingleOrDefault(expression.Compile());
}
public void Delete(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List.Remove(key);
}
}
You would not mock the repository; you would have an alternate repository that would use an in-memory store instead of the database, then use IoC to select the correct repository implementation for tests/code.
This blog article might be of use, although my design has changed somewhat since I wrote the post and I really need to update it. I used teh generic repository pattern in a way that enables the DbContext to be mocked. This allows the data access layer to be tested 'right up to the edges'.
Times have changed -- since the release of Entity Framework 6 it has become much easier to mock database context and datasets. This article outlines the particulars.
Testing non-query scenarios
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Data.Entity;
namespace TestingDemo
{
[TestClass]
public class NonQueryTests
{
[TestMethod]
public void CreateBlog_saves_a_blog_via_context()
{
var mockSet = new Mock<DbSet<Blog>>();
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(m => m.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
service.AddBlog("ADO.NET Blog", "http://blogs.msdn.com/adonet");
mockSet.Verify(m => m.Add(It.IsAny<Blog>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
}
}
}
Testing query scenarios
Query testing is pretty sweet now, because you can build up test data sets in code and then execute your tests against them:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace TestingDemo
{
[TestClass]
public class QueryTests
{
[TestMethod]
public void GetAllBlogs_orders_by_name()
{
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(0 => data.GetEnumerator());
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(c => c.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blogs = service.GetAllBlogs();
Assert.AreEqual(3, blogs.Count);
Assert.AreEqual("AAA", blogs[0].Name);
Assert.AreEqual("BBB", blogs[1].Name);
Assert.AreEqual("ZZZ", blogs[2].Name);
}
}
}
You can do this by mocking the DbSet.Add() method, like so:
[Fact]
public void CreateBlog_saves_a_blog_via_context()
{
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
};
var mockSet = new Mock<DbSet<Blog>>();
mockSet.Setup(blogs => blogs.Add(It.IsAny<Blog>)).Returns<Blog>(blog =>
{
data.Add(blog);
return blog;
});
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(m => m.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blog = service.AddBlog("_ADO.NET Blog", "http://blogs.msdn.com/adonet");
var blogs = service.GetAllBlogs();
mockSet.Verify(m => m.Add(It.IsAny<Blog>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
Assert.NotNull(blog)
Assert.Equal(4, blogs.Count);
Assert.Equal("AAA", blogs(1).Name);
Assert.Equal("BBB", blogs(2).Name);
Assert.Equal("ZZZ", blogs(3).Name);
}
This is adapted from the documentation found here.