vendredi 22 janvier 2016

Dynamically test fields translated through AutoMapper

I want to map from class A to class B. However, class A has a few property name mismatches with class B and has quite a few nullable types. In the case of a null, I'm supposed to provide a default value for that field (some int?s should be 0, some should be 1, etc). It seems like I've not got a daisy chain of about 15 ForMember calls stuck onto my Mapper.CreateMap(). (If there's a better way to do this, let me know).

Now I'm wondering how in the world I can test this concisely. I was thinking I could leverage NUnit's TestCaseSource functionality, so create a bunch of As, and basically create 2 tests for every single field e.g.

// given int? A.foo == null
Assert.True(B.fooVal == DEFAULT_FOO_VALUE)
// another test...
// given int? A.foo == 500
Assert.True(B.fooVal == 500)

I'm just not sure how to do this dynamically (or if it is even possible). So there's four inputs to the test case, A's field under test, A's field under test's value, B's field under test, B's field under test's value.

I could do this all by hand but I feel like there's probably some much smarter solution.

Aucun commentaire:

Enregistrer un commentaire