问题
I need to choose mock framework to new project.
What are the pros and cons for those frameworks? Any comparison table?
I know that JustMock is i beta stage but it's look very good right now (very similar to TypeMock)
Edit: I'v What about MS Mole?
回答1:
Before there was JustMock this question was asked and the answers can be found here. There is a very good Mocking framework comparison - it doesn't have JustMock yet but you get to see the syntax and capabilities of each .NET mocking framework.
RhinoMocks (and Moq) are both open source free to use projects that can create fake objects by inheriting the type - which means that they can not fake static methods, structs and sealed classes.
Typemock Isolator uses the CLR profiler to intercept .NET calls and change them during runtime and can fake virtually anything. Typemock can also fake objects that you cannot "reach" from the test because they are instantiated inside the production code. Some call it "black magic" but others find the ability to fake virtually anything - including 3rd party libraries and legacy code - a must.
JustMock is a new player and right now only in Beta - it has some of Typemock Isolator and can run without the profiler API - in this mode it's just like RhinoMocks. It is yet to be discovered how much of Typemock Isolator capabilities does it really have. Right now it seems that its a good start in the right direction but it still have some distance to go before it could be a valid option.
回答2:
I recently switched from RhinoMocks to Moq.
Both have similar capabilities but Moq has much cleaner API
回答3:
[Disclaimer - I work at Telerik]
Just a few points about JustMock. Its API is more similar to Moq's API than RhinoMocks', so if you’re familiar with Moq you shouldn’t have problems learning JustMock. What we've tried to do better in JustMock is to have a better story when mocking sequential calls for example. Additionally as someone already mentioned JustMock makes it possible to mock static classes and methods / properties, sealed classes, non-virtual methods and properties and so on using the same Moq-like syntax. Also thanks to this in JustMock it’s possible to do partial mocking.
Btw, the "black magic" that Dror mentioned is also possible when using JustMock if you reference directly the Telerik.CodeWeaver.Hook assembly but unfortunately that's not documented at the moment. Should someone want to do that, he could open a support ticket and we'll be glad to help.
Hope this helps.
回答4:
Also checkout Microsoft Moles, an isolation framework for .NET that allows to replace any .NET method with a custom delegate.
Moles has a version that can be used for commercial projects and is supported in Visual Studio 2008 and 2010.
回答5:
Also see: https://stackoverflow.com/questions/37359/what-c-mocking-framework-to-use/2608634#2608634
I'm currently Unit Testing in Moq, and I must say it works pretty well! - rephrase: It works great! I've not used any of the other mocking frameworks you mention so I can't give you a comparison. But I can say that I'm glad that I've chosen Moq as my first-to-try mocking framework. The lamda expressions are really nice and it's also pretty lightweight and reader friendly (the record/replay syntax in most other mocking frameworks aren't really doing your readability any good)
Besides that (and this is a bit off-topic) I will be using Ninject in the near future as IoC Container, and both frameworks go hand-in-hand. Ninject also has Lamda's and it even provides auto-mocking container support for Moq (using an extension). So if you're also planning to use an IoC container you could check this awesome combination :]
回答6:
I won't tell you which is the best becasue each has its pros and cons. I always used Rhino Mocks because it's free and there is a great community around and easy to find any help.
TypeMock is not free (I guess). However TypeMock is great if you want for example to test the legacy code with private or static types an members. Other mocking frameworks can't do it. A drawback is that it doesn't force you to write testable code which has other benefits that just the testability.
I don't know JustMock so can't tell you how it works.
I would advice you to try them all and to pick un one with which you feel comfortable. However if you need to test legacy code, chose than TypeMock.
I hope others will give you more details.
来源:https://stackoverflow.com/questions/2637066/rhino-mock-vs-typemock-vs-justmock-vs