我是.net核心/C#编程新手(来自Java)
我有下面的Service,它使用依赖项注入获取AutoMapper对象和数据存储库对象,用于创建SubmissionCategoryViewModel对象的集合:
public class SubmissionCategoryService : ISubmissionCategoryService
{
private readonly IMapper _mapper;
private readonly ISubmissionCategoryRepository _submissionCategoryRepository;
public SubmissionCategoryService(IMapper mapper, ISubmissionCategoryRepository submissionCategoryRepository)
{
_mapper = mapper;
_submissionCategoryRepository = submissionCategoryRepository;
}
public List<SubmissionCategoryViewModel> GetSubmissionCategories(int ConferenceId)
{
List<SubmissionCategoryViewModel> submissionCategoriesViewModelList =
_mapper.Map<IEnumerable<SubmissionCategory>, List<SubmissionCategoryViewModel>>(_submissionCategoryRepository.GetSubmissionCategories(ConferenceId) );
return submissionCategoriesViewModelList;
}
}我正在使用Xunit编写单元测试。我不知道如何为方法GetSubmissionCategories编写单元测试,并让我的测试类提供一个IMapper实现和一个ISubmissionCategoryRepository实现。
到目前为止,我的研究表明,我可以创建依赖对象的测试实现(例如SubmissionCategoryRepositoryForTesting),也可以使用模拟库来创建依赖关系接口的模拟。
但我不知道如何创建AutoMapper的测试实例或AutoMapper的模拟。
发布于 2018-04-07 15:44:17
这段代码应该为您提供了一个优势:
[Fact]
public void Test_GetSubmissionCategories()
{
// Arrange
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new YourMappingProfile());
});
var mapper = config.CreateMapper();
var repo = new SubmissionCategoryRepositoryForTesting();
var sut = new SubmissionCategoryService(mapper, repo);
// Act
var result = sut.GetSubmissionCategories(ConferenceId: 1);
// Assert on result
}https://stackoverflow.com/questions/49708895
复制相似问题