简体   繁体   English

我真的必须以不同的方式编写我的代码,以便可以测试它吗?

[英]Do I really have to do write my code differently so just so I can test it?

I am coming to c# from ruby on rails where I did TDD using Rspec and Mocha. 我是从使用ruby在Rspec和Mocha进行TDD的Rails上来到c#的。 I was looking to get into get into a mocking framework and an older stackoverflow post pointed me in the direction of MOQ and rhino mocks. 我当时想进入一个模拟框架,一个较老的stackoverflow帖子向我指出了最小起订量和犀牛模拟的方向。 I was hesitant to commit to either and am looking for a bit of advice or clarification or maybe just a pep talk... 我很犹豫要不要这样做,并且正在寻找一些建议或澄清,或者只是一个鼓舞人心的谈话...

Here were my concerns: 这是我的担忧:

  • Rhino mocks seems to be a bit out of date. 犀牛模型似乎有些过时了。 A lot of the examples didn't even make use of generics because it seems like they weren't available when the framework was created 许多示例甚至都没有使用泛型,因为当创建框架时,它们似乎不可用
  • Moq seems to be less powerful than Rhino mocks and it seems like you need to adapt your classes in order to mock them (either they must implement an interface or all methods must be virtual) Moq似乎不如Rhino模拟强大,并且似乎需要调整类以模拟它们(它们必须实现接口或所有方法必须是虚拟的)

Am I way off base here? 我要离开这里吗? Am I missing something? 我想念什么吗? Is there some cool new framework I should know about or do I just need to open my mind up and accept that mocking isn't the same for static languages? 我是否应该了解一些很酷的新框架,还是只需要敞开心mind接受那些静态语言的模拟是不一样的?

(I know questions kinda like this have been asked in the past but they seem a little dated to me and I am interesting in what the latest hip new things are) (我知道过去曾问过类似的问题,但对我来说似乎有些过时,我对最新的时髦事物很感兴趣)

I personally vote for moq, but selection of mocking framework is really up to you. 我个人投票同意起订量,但是选择模拟框架确实取决于您。 I also think that testability is good enough reason to write your code differently. 我还认为,可测试性足以作为编写不同代码的理由。 Interfaces in general are quite hard to overuse and in the long run they probably make your code more maintainable. 通常,接口很难被过度使用,从长远来看,它们可能会使您的代码更具可维护性。

Test-driven development doesn't have a side-effect of making you write your code in a different way. 测试驱动的开发没有使您以其他方式编写代码的副作用。 It is actually intended to make you write code differently. 实际上,它旨在使您以不同的方式编写代码。 We think that you write code better when you test it. 我们认为您在测试代码时会写得更好。 You are more likely to hide code behind an interface, which is a good thing. 您更有可能将代码隐藏在界面后面,这是一件好事。 It will hopefully make you do lots of stuff in a different way! 希望它将使您以不同的方式做很多事情!

Onto Rhino / Moq... 上Rhino / Moq ...

Rhino Mocks has received a bit overhaul since it was first written, you no longer have to use the record/replay syntax, you can use Arrange-Act-Assert syntax and there are no magic strings to be seen. 自从首次编写Rhino Mocks以来,已经进行了一些大修,您不再需要使用记录/重播语法,可以使用Arrange-Act-Assert语法,而且没有魔术字符串可看。

Rhino Mocks - Arrange Act Assert Syntax 犀牛Mo-编排言断句法

In Moq you can mock interfaces and classes. 在Moq中,您可以模拟接口和类。 The syntax is simpler and a bit more expressive. 语法更简单,更具表现力。

The good news is, I think you are looking at the best two and trying to choose between them. 好消息是,我认为您正在寻找最佳的两个,并试图在它们之间进行选择。 I don't think you would regret selecting either of these. 我认为您不会后悔选择其中任何一个。

暂无
暂无

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

相关问题 如何在 [Setup] 或 [OneTimeSetup] 中传递参数,以便我不必在测试类中调用该方法? - How can I pass parameters in a [Setup] or [OneTimeSetup] so I so not have call the method in my test class? 我必须如何更改我的代码才能创建 3x3 字段? - How do I have to change my code so that a 3x3 field is created? 我如何在设置中使用字符串文件路径,以便我可以在我的代码中使用该路径 - How do i use a string file path in settings so i can get that path useable in my code 如何重构我的 C# 代码,以免重复自己? - How can I refactor my C# code so I do not repeat myself? 我如何授予权限以便我的代码可以创建新文档 - How do I give privileges so my code can create new documents “Stack overflow. Repeat 261929 times[...]”是错误消息,当我调试我的代码然后我没有看到任何问题时我应该怎么做? - "Stack overflow. Repeat 261929 times[...]" is the error message when I have debugged my code then I do not see any issues so what should I do? 如何在C#中将ASCII小数转换为字符串。 这是我到目前为止的代码: - How do I convert ASCII decimals to a string in C#. This is the code I have so far: 如何在单元测试中模拟控制器上下文,以便我对字符串函数的部分视图有效? - How do I mock controller context in my unit test so that my partial view to string function works? 翻译DataGrid MouseEvent,以便在View的Code-Behind中没有代码 - Translate DataGrid MouseEvent so I do not have code in View's Code-Behind 如何使用 XUnit `InlineData` 以便为 MultipartFormDataContent 插入多个测试数据? - How do I use XUnit `InlineData` so that I can insert multiple test data for MultipartFormDataContent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM