简体   繁体   English

在进行TDD时,处理转换方法的最佳方法是什么?

[英]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. 问题在于,似乎只是在编写额外的代码来进行转换,并且在配置IOC容器时需要考虑很多新的依赖项。

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? 最佳做法是什么?

You can Moq for more information, how to use the moq with TDD check the below link 您可以通过Moq获取更多信息,如何将Moq与TDD一起使用请检查以下链接

TDD : Introduction to Moq TDD:Moq简介

And also for for more info about TDD check below links 有关TDD的更多信息,请参见下面的链接

TDD/BDD screencast/video resources TDD / BDD截屏/视频资源

Improve the Design and Flexibility of Your Project with Extreme Programming Techniques 使用极限编程技术改善项目的设计和灵活性

Best Practices of Test Driven Development Using C# and RhinoMocks 使用C#和RhinoMocks进行测试驱动开发的最佳实践

I would use Automapper . 我会使用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 :) 您可以编写一些非常基本的单元测试,以确保您的转换始终有效,但是那可能不是您要进行单元测试的其他事情的优先事项:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM