Oved D
Oved D

Reputation: 7442

What is the best way to deal with conversion methods when doing TDD?

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?

Upvotes: 1

Views: 240

Answers (2)

Peyman
Peyman

Reputation: 3138

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

Upvotes: 1

ericb
ericb

Reputation: 3410

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 :)

Upvotes: 0

Related Questions