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. 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. 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开发者_如何学JAVA 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)
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?
(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)
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 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 Syntax
In Moq you can mock interfaces and classes. 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.
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.
精彩评论