I'm writing unit test for my UserRepository
class where I'm using AutoMapper ProjectTo<T>
Queryable Extensions for projection. Here is how the class looks like:
public class UserRepository:IUserRepository
{
private readonly UserManager<UserEntity> _userManager;
private readonly IConfigurationProvider _mappingConfiguration;
public UserRepository(
UserManager<UserEntity> userManager,
IConfigurationProvider mappingConfiguration)
{
_userManager = userManager;
_mappingConfiguration = mappingConfiguration;
}
public async Task<IEnumerable<User>> GetUsersAsync()
{
IQueryable<UserEntity> query = _userManager.Users;
var size = await query.CountAsync();
var items = await query
.ProjectTo<User>(_mappingConfiguration)
.ToArrayAsync();
return items;
}
}
I'm using x-unit unit test project. Here is my test class,
public class UserRepositoryTests
{
[Fact]
public async void GetUsers_AtleastOne_ReturnOneOrMore()
{
// Arrange
var connectionStringBuilder =
new SqliteConnectionStringBuilder { DataSource = ":memory:" };
var connection = new SqliteConnection(connectionStringBuilder.ToString());
var options = new DbContextOptionsBuilder<GallaContext>()
.UseSqlite(connection)
.Options;
var mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
var mockUserManager = new Mock<UserManager<UserEntity>>(new Mock<IUserStore<UserEntity>>().Object,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<UserEntity>>().Object,
new IUserValidator<UserEntity>[0],
new IPasswordValidator<UserEntity>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<UserEntity>>>().Object);
var mockAutoMapper = new Mock<IConfigurationProvider>();
using (var context = new GallaContext(options, mockHttpContextAccessor.Object))
{
context.Database.OpenConnection();
context.Database.EnsureCreated();
var userRepository = new UserRepository( mockUserManager.Object, mockAutoMapper.Object);
// Act
var users = await userRepository.GetUsersAsync();
// Assert
users.Should().HaveCountGreaterOrEqualTo(1);
}
}
}
I'm getting the following error when executing ProjectTo<User>
in my repository class.
{System.NullReferenceException: Object reference not set to an instance of an object. at AutoMapper.QueryableExtensions.ProjectionExpression.ToCore[TResult](Object parameters, IEnumerable`1 memberPathsToExpand)
I'm new to Moq and Unit Testing, I searched a lot but couldn't find a way to Mock this. Please assist on how to properly Mock AutoMapper IConfigurationProvider
along with my MappingProfile
Here is the Error Details
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=AutoMapper
StackTrace:
at AutoMapper.QueryableExtensions.ProjectionExpression.ToCore[TResult](Object parameters, IEnumerable '1 memberPathsToExpand)
at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Expression`1[] membersToExpand)
at Repositories.UserRepository.<GetUsersAsync>d__5.MoveNext() in ..path\:line 46
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter '1.GetResult()
at Repositories.Test.UserRepositoryTests.<GetUsers_AtleastOne_ReturnOneOrMore>d__0.MoveNext() in ..path\Repositories.Test\UserRepositoryTests.cs:line 53
Thanks,
Abdul
Here is how I resolved following @Fabio comments and that works.
var mockAutoMapper = new MapperConfiguration(mc => mc.AddProfile(new MappingProfile())).CreateMapper().ConfigurationProvider;
User contributions licensed under CC BY-SA 3.0