4

I'm new to .net core/C# programming (coming over from Java)

I have the following Service class the that uses dependency injection to get an AutoMapper object and a data repository object for use in creating a collection of SubmissionCategoryViewModel objects:

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;


    }
}

I'm writing my unit tests using Xunit. I cannot figure out how to write a unit test for method GetSubmissionCategories and have my test class supply an IMapper implementation and a ISubmissionCategoryRepository implementation.

My research so far indicates that I could either create a test implementation of the dependent objects (e.g. SubmissionCategoryRepositoryForTesting) or I can use a mocking library to create a mock of the dependency interface.

But I don't know how I would create a test instance of AutoMapper or a mock of AutoMapper.

ruffin
  • 16,507
  • 9
  • 88
  • 138
Bruce Phillips
  • 2,740
  • 3
  • 18
  • 17
  • Alas asking for off site resources is considered off topic. – Nkosi Apr 07 '18 at 15:34
  • with a mocking library you can mock the interfaces, inject them into the subject under test and exercise the method under test to assert that it behaves as expected. – Nkosi Apr 07 '18 at 15:36
  • Show how you tried to test the above code. From there we should be able to steer you in the right direction. – Nkosi Apr 07 '18 at 15:39
  • Here is an example close to what you are asking https://stackoverflow.com/a/48387824/5233410 – Nkosi Apr 07 '18 at 15:46
  • Have edited the question to remove the explicit request for a book or tutorial. **Note also** that it's generated an accepted, high-quality answer that does not include any such recommendation. Should be reopened. – ruffin Sep 15 '20 at 20:31

1 Answers1

24

This snippet should provide you with a headstart:

[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
}
junkangli
  • 1,152
  • 7
  • 14
  • above was very helpful and was enough to show me how to write the unit test. I did need to add ); to the end of the config statement – Bruce Phillips Apr 07 '18 at 16:37
  • @junkangli can you answer this question ? https://stackoverflow.com/questions/57331395/net-core-execute-all-dependency-injection-in-xunit-test-for-appservice-reposit?noredirect=1&lq=1 –  Aug 03 '19 at 03:40