作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的 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;
}
}
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)
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/
我正在为我的 UserRepository 编写单元测试我使用 AutoMapper 的类(class) ProjectTo可查询的投影扩展。下面是这个类的样子: public class UserR
我将我的 ASP.NET 5 项目从 Beta 7 更新到 Beta 8,但收到以下错误消息。 Error CS7069: Reference to type 'IConfigurationProvi
我是一名优秀的程序员,十分优秀!