如何为使用AutoMapper和dependency injection的.net core 2.0服务编写xUnit测试?

我是.net核心/ C#编程的新手(来自Java)

我有以下Service类,它使用dependency injection来获取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 GetSubmissionCategories(int ConferenceId) { List submissionCategoriesViewModelList = _mapper.Map<IEnumerable, List>(_submissionCategoryRepository.GetSubmissionCategories(ConferenceId) ); return submissionCategoriesViewModelList; } } 

我正在使用Xunit编写unit testing。 我无法弄清楚如何为方法GetSubmissionCategories编写unit testing,并让我的测试类提供IMapper实现和ISubmissionCategoryRepository实现。

到目前为止,我的研究表明我可以创建依赖对象的测试实现(例如SubmissionCategoryRepositoryForTesting),或者我可以使用模拟库来创建依赖接口的模拟。

但我不知道如何创建AutoMapper的测试实例或AutoMapper的模拟。

如果你知道任何好的在线教程,详细介绍如何创建一个unit testing,其中被测试的类使用AutoMapper和dependency injection一个很好的数据存储库。

感谢您的帮助。

这个片段应该为您提供一个开端:

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