5

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.

5
  • Alas asking for off site resources is considered off topic. Commented Apr 7, 2018 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. Commented Apr 7, 2018 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. Commented Apr 7, 2018 at 15:39
  • Here is an example close to what you are asking stackoverflow.com/a/48387824/5233410 Commented Apr 7, 2018 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. Commented Sep 15, 2020 at 20:31

1 Answer 1

27

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
@junkangli can you answer this question ? stackoverflow.com/questions/57331395/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.