[英]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: 这是我的担忧:
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.