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

◇◆丶佛笑我妖孽 提交于 2019-12-11 04:24:11

问题


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?


回答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




回答2:


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



来源:https://stackoverflow.com/questions/6906994/what-is-the-best-way-to-deal-with-conversion-methods-when-doing-tdd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!