1

What is the best practice is for dealing with conversions that occur in methods when trying to do test driven development?

Is it to create static utility methods that perform the conversions and then write unit tests on those utility methods? I feel like the problem with that is when you write tests against the parent method that calls this utility method, you have to account for the conversion occurs, since most mocking frameworks do not mock utility methods. Therefore, writing verification methods that deal with the parent method becomes difficult.

The other option I've thought of would be to create an interface responsible for the conversions, and mock out that interface when testing the calling method. There would be an implementation sitting next to the interface. The problem with this is it seems like extra code is being written just to do the conversions, and a lot of new dependencies need to be accounted for when configuring the IOC container.

A perfect example is a controller action that needs to convert from the view model to the entity that is the input for the service it connects to. What's best practice for this?

Oved D
  • 7,132
  • 10
  • 47
  • 69

2 Answers2

1

You can Moq for more information, how to use the moq with TDD check the below link

TDD : Introduction to Moq

And also for for more info about TDD check below links

TDD/BDD screencast/video resources

Improve the Design and Flexibility of Your Project with Extreme Programming Techniques

Best Practices of Test Driven Development Using C# and RhinoMocks

Community
  • 1
  • 1
Peyman
  • 3,068
  • 1
  • 18
  • 32
0

I would use Automapper. It's built to handle exactly this scenario and most of its core is already unit tested. You could write some very basic unit tests to ensure that your conversion always works, but thats probably not priority to other things you are trying to unit test :)

ericb
  • 3,400
  • 1
  • 21
  • 21
  • Are the mapping methods in Automapper called through an interface? Or would I have to create a wrapper around it? – Oved D Aug 03 '11 at 03:53
  • It is possible to mock Automapper [http://richarddingwall.name/2009/05/07/mocking-out-automapper-with-dependency-injection/] – the_joric Aug 03 '11 at 08:45
  • the_joric, it's a broken link :( – Oved D Aug 03 '11 at 12:20
  • Looked at the source, and it doesn't look like `Mapper.Map(source,dest)` has any interface to it. However, I'm not exactly sure why you would want to mock/wrap these method calls... You would probably want to unit test these directly as part of your controller tests, etc... Any more background on why you are specifically trying to mock/wrap these? – ericb Aug 03 '11 at 16:41