gpt4 book ai didi

c# - 如何在asp.net核心中的x单元单元测试中模拟AutoMapper IConfigurationProvider

转载 作者:行者123 更新时间:2023-12-04 01:42:23 25 4
gpt4 key购买 nike

我正在为我的 UserRepository 编写单元测试我使用 AutoMapper 的类(class) ProjectTo<T>可查询的投影扩展。下面是这个类的样子:

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;
}
}

我正在使用 x-unit 单元测试项目。这是我的测试课,
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);
}
}
}

执行 ProjectTo<User> 时出现以下错误在我的存储库类中。

{System.NullReferenceException: Object reference not set to an instance of an object. at AutoMapper.QueryableExtensions.ProjectionExpression.ToCore[TResult](Object parameters, IEnumerable`1 memberPathsToExpand)



我是 Moq 和单元测试的新手,我搜索了很多,但找不到模拟这个的方法。请协助如何正确模拟 AutoMapper IConfigurationProvider连同我的 MappingProfile
这是错误详细信息
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

谢谢,

阿卜杜勒

最佳答案

这是我按照@Fabio 评论解决的方法,并且有效。

var mockAutoMapper = new MapperConfiguration(mc => mc.AddProfile(new MappingProfile())).CreateMapper().ConfigurationProvider;

关于c# - 如何在asp.net核心中的x单元单元测试中模拟AutoMapper IConfigurationProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com