Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Obviously, you have to test mapping code somehow, even (or especially) if you use AutoMapper. Is there any way to make it less verbose?

[Test]
public void Map_Always_SetsSimpleProperties()
{
  var auctionPlace = fixture.Create<string>();
  var submissionCloseDateTime = fixture.Create<DateTime>();
  var quotationForm = fixture.Create<string>();
  var quotationExaminationDateTime = fixture.Create<DateTime>();
  var envelopeOpeningTime = fixture.Create<DateTime>();
  var envelopeOpeningPlace = fixture.Create<string>();
  var auctionDateTime = fixture.Create<DateTime>();
  var applExamPeriodDateTime = fixture.Create<DateTime>();
  var considerationSecondPartDate = fixture.Create<DateTime>();

  var doc = fixture.Create<Notification223>();

  doc.AuctionPlace = auctionPlace;
  doc.SubmissionCloseDateTime = submissionCloseDateTime;
  doc.QuotationForm = quotationForm;
  doc.QuotationExaminationTime = quotationExaminationDateTime;
  doc.EnvelopeOpeningTime = envelopeOpeningTime;
  doc.EnvelopeOpeningPlace = envelopeOpeningPlace;
  doc.AuctionTime = auctionDateTime;
  doc.ApplExamPeriodTime = applExamPeriodDateTime;
  doc.ConsiderationSecondPartDate = considerationSecondPartDate;

  var sut = CreateSut();

  var actual = sut.Map(doc);

  Assert.That(actual.AuctionPlace, Is.EqualTo(auctionPlace));
  Assert.That(actual.SubmissionCloseDateTime, Is.EqualTo(submissionCloseDateTime));
  Assert.That(actual.QuotationForm, Is.EqualTo(quotationForm));
  Assert.That(actual.QuotationExaminationDateTime, Is.EqualTo(quotationExaminationDateTime));
  Assert.That(actual.EnvelopeOpeningTime, Is.EqualTo(envelopeOpeningTime));
  Assert.That(actual.EnvelopeOpeningPlace, Is.EqualTo(envelopeOpeningPlace));
  Assert.That(actual.AuctionDateTime, Is.EqualTo(auctionDateTime));
  Assert.That(actual.ApplExamPeriodDateTime, Is.EqualTo(applExamPeriodDateTime));
  Assert.That(actual.ConsiderationSecondPartDate, Is.EqualTo(considerationSecondPartDate));
}
share|improve this question
1  
If you put one assertion per test it doesn't look that verbose. –  abuzittin gillifirca Jan 31 '14 at 11:34

1 Answer 1

up vote 2 down vote accepted

Unfortunately, I don't think so.

There is a question about this topic on the website of Dozer (which is a similar library for Java) which mentions a trick:

Should I write unit tests for data mapping logic that I use Dozer to perform?

[...]

Regardless of whether or not you use Dozer, unit testing data mapping logic is tedious and a necessary evil, but there is a trick that may help. If you have an assembler that supports mapping 2 objects bi-directionally, in your unit test you can do something similar to the following example. This also assumes you have done a good job of implementing the equals() method for your data objects. The idea is that if you map a source object to a destination object and then back again, the original src object should equal the object returned from the last mapping if fields were mapped correctly. [...]

I think this trick won't find bugs when, for example, doc.ApplExamPeriodTime is mapped to actual.ConsiderationSecondPartDate and doc.ConsiderationSecondPartDate is mapped to actual.ApplExamPeriodTime. I don't know that these kind of bugs are possible with AutoMapper or not.

Furthermore, if you need only one directional mapping, I think the most simple solution is the one that's already in your question, I'd go with that. Adding code for the reverse mapping to the production code would be a test smell (Test Logic in Production).

share|improve this answer
    
Well, what if I actually need only one direction? I strongly dislike the idea of putting something in a public API just for sake of testing. –  vorou Jan 30 '14 at 3:36
    
@vorou: I think in that case the only solution is that that you showed in your question. What method do you think of as putting it public for only testing? –  palacsint Jan 30 '14 at 21:15
    
I'm talking about reverse mapping, I don't have such method right now. –  vorou Jan 31 '14 at 9:22
    
@vorou: I've update the answer a little bit. –  palacsint Feb 1 '14 at 10:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.