There are lots of mocking frameworks out there for .Net. There is no clear winner that has superseded the others in every way. The leading mocking frameworks also have many different styles of usage.
对于。net,有很多模仿框架。在任何方面,都没有明显的赢家可以取代其他赢家。主要的mock框架也有许多不同的用法样式。
The time it takes to learn all of the mocking frameworks well enough to decide which to use is unreasonable. I don’t believe that we have yet reached a stage that we can talk about the best mocking framework. So what questions should I be asking, both about the project, and about myself, to help decide on the best mocking framework to use, in a given case?
学习所有mock框架以确定使用哪个是不合理的。我认为我们还没有达到可以讨论最好的mock框架的阶段。所以我应该问的是,关于这个项目,关于我自己,在一个特定的情况下,帮助决定使用最好的模拟框架?
It would also be useful to know why you chose the mocking framework you are currently using and if you are still happy with that choice.
如果知道为什么选择了当前正在使用的mock框架,如果您仍然对这个选择感到满意,这也很有用。
Is there a useful vocabulary to use when comparing the styles of mocking frameworks?
在比较mock框架的样式时,是否需要使用有用的词汇表?
(Note: I have limited this question to .Net as Java does not have attributes or lambda expression, so I hope the mocking frameworks can be better for .Net then Java)
(注意:由于Java没有属性或lambda表达式,所以我将这个问题限制在。net中,所以我希望mock框架能比Java更好)
Summary so far:
总结:
- If you need to mock static method, or none virtual methods then the only reasonable option is TypeMock, however it is not free and does not drive you towards a good design.
- 如果您需要模拟静态方法,或者没有虚拟方法,那么唯一合理的选择就是TypeMock,但是它不是免费的,并且不会将您引向好的设计。
- Rhino Mocks is a very good option if you are doing TDD, .e.g the objects you wish to mock implement interfaces. At present it seems to be the "market leader"
- 如果您正在做TDD,那么Rhino模拟是一个很好的选择。g希望模拟实现接口的对象。目前它似乎是“市场领导者”
- Moq (introduction) should be considered if you are using .NET 3.5 Moq may be gaining on Rhino Mocks for new projects
- 如果您正在使用。net 3.5 Moq,那么应该考虑Moq (introduction)
What have I missed from this summary?
我从这个总结中漏掉了什么?
So what drives the choice between Rhino Mocks and Moq, if you are using .NET 3.5?
那么,如果你正在使用。net 3.5,是什么驱使你在Rhino mock和Moq之间做出选择呢?
see also:
参见:
- What C# mocking framework to use?
- 使用什么c# mock框架?
- What are the capabilities of Moq and Rhino.mocks?
- Moq和Rhino.mocks的功能是什么?
- What are the real-world pros and cons of each of the major mocking frameworks?
- 每个主要mock框架的实际优缺点是什么?
“What should I consider when choosing a dependency injection framework for .NET?” may also be of interest as it asks the “other side” of the question.
在为。net选择依赖注入框架时,我应该考虑什么?当它问问题的“另一面”时,可能也会引起人们的兴趣。
8 个解决方案
#1
21
So what questions should I by asking about the project and myself to help decide on the best mocking framework to use in a given case?
那么,我应该通过问关于项目和我自己的什么问题来帮助决定在给定的情况下使用什么样的mock框架呢?
The questions you should be asking about the project is: Has the project been developed with the SOLID principles, or not? Is this a project that has loose coupling and high cohesion? Have good OO principles been utilized in building the project? Is a Dependency Injection container being utilized? Has the system been coded in a Design by Contract method (utilizing Interfaces thoroughly)?
关于这个项目,你应该问的问题是:这个项目是否有坚实的原则?这是一个具有松散耦合和高内聚性的项目吗?在构建项目时是否使用了良好的OO原则?是否正在使用依赖注入容器?系统是否用契约方法(充分利用接口)进行设计编码?
If you answer yes to these questions, then you can utilize a mocking framework like RhinoMocks, which is what some would call an "opinionated" framework. RhinoMocks, and some other mocking frameworks, have very strong opinions about how a system should be designed in order for objects to be mocked. A framework like RhinoMocks expects your project to be designed a certain way. Mocking is certainly a lot easier with RhinoMocks when you've built your code the right way (no sealed classes, no statics, heavy use of interfaces, virtual on class methods, etc.)
如果你对这些问题的回答是“是”,那么你可以使用一个像RhinoMocks这样的嘲弄框架,这就是一些人所说的“固执己见”的框架。RhinoMocks和其他一些mock框架对于如何设计一个系统以便对对象进行模拟有着非常强烈的看法。像RhinoMocks这样的框架期望您的项目以某种方式设计。当你以正确的方式构建你的代码(没有封闭的类,没有静态,大量使用接口,虚拟在类方法等等)时,用RhinoMocks来模拟肯定要容易得多。
If you answer no to those questions, or if you're working on a legacy system with a lot of highly coupled classes, then your only choice is going to be TypeMock, which can mock just about anything.
如果您对这些问题的回答是否定的,或者如果您正在处理一个具有大量高耦合类的遗留系统,那么您惟一的选择就是TypeMock,它可以模拟任何事情。
It would also be useful to know why you choose the mocking framework you are currently using and if you are still happy with that choose.
了解为什么选择当前使用的mock框架,以及您是否仍然满意这个选择,也将是非常有用的。
I chose RhinoMocks because at the time (3+ years ago) it was clearly the most mature mocking framework with the most features. I've stayed with it because it has evolved in away that makes my life much easier (the advent of the AutoMocking container being a gigantic step toward efficiency).
我选择RhinoMocks是因为在当时(3年多以前),它显然是最成熟的mock框架,具有最多的特性。我一直坚持使用它,因为它的发展让我的生活变得更容易(自动容器的出现是向效率迈出的巨大一步)。
What I like about RhinoMocks, other than the feature set and ease of use, is that it guides me toward a better design in my code. I am not a perfect programmer, and I am going to make mistakes in design. But tools like RhinoMocks and NHibernate help guide me toward a better design, because when I do make mistakes and create poor design, these tools become painful to work with. NHibernate, for instance, is painful to work with if you have a bad database design. RhinoMocks is very painful to work with if you have a poor class design, aren't using interfaces, aren't using IoC... etc.
我喜欢RhinoMocks,除了它的特性集和易用性,它引导我在我的代码中更好的设计。我不是一个完美的程序员,我将会在设计上犯错误。但是,像RhinoMocks和NHibernate这样的工具可以帮助我实现更好的设计,因为当我犯错误并创建糟糕的设计时,这些工具就会变得难以使用。例如,如果您的数据库设计不好,那么使用NHibernate会很痛苦。如果你的类设计不好,没有使用接口,没有使用IoC,那么使用RhinoMocks是非常痛苦的。等。
I like RhinoMocks because it ultimately helps me be a better developer, and not just because I'm testing my code, but because I'm shaping my code - designing it - in a better manner.
我喜欢RhinoMocks,因为它最终帮助我成为一个更好的开发人员,不仅仅是因为我正在测试我的代码,还因为我正在以更好的方式修改我的代码——设计它。
#2
14
I prefer Moq for .NET 3.5 projects. It is simple to use and, in my experience, it helps to produce clean and clear unit tests.
我更喜欢。net 3.5项目的Moq。它使用起来很简单,而且根据我的经验,它有助于生成清晰的单元测试。
There's no technical reason why you can't use more than one mocking framework on the same project. Sure, it's nice to standardize, but some tests may lend themselves better to some frameworks.
在同一个项目中,不能使用多个mock框架,这是没有技术原因的。当然,标准化很好,但是有些测试可能会更好地应用于某些框架。
#3
10
Disclaimer – I work for Typemock
免责声明:我为排印机工作
I disagree with "Typemock does not drive you to good design". It is you, the developer that drives a good or bad design, not the tool that you use. Get the tool that gets the job done, and make you productive, but the responsibility of good design is your own. If you think that wrapping all kind of abstractions inside your code, just for testing, go for it, but you may not come up with a "good" design. It could be more complex than the design you have right now.
我不同意“类型锁不会让你得到好的设计”。是你自己,开发人员在驱动一个好的或坏的设计,而不是你使用的工具。让工具完成工作,使你富有成效,但是好的设计的责任是你自己的。如果您认为在代码中包装所有类型的抽象,只是为了测试,那么就去做,但是您可能不会想到一个“好的”设计。它可能比你现在的设计还要复杂。
#4
8
What should I consider when choosing a mocking framework for .Net
在为。net选择一个mock框架时,我应该考虑什么
Another aspect to consider, besides the design of the code base that you are testing, is how readable your tests are when using mocks.
另外一个需要考虑的方面是,除了您正在测试的代码库的设计之外,您的测试在使用模拟时是多么的易读。
In this regard, I am putting in a vote for NSubstitute - one of the newer mocking frameworks with a very expressive API (no lambdas) and comparable functionality with Moq and Rhino Mocks. It has recently reached version 1.0.
在这方面,我将投票支持NSubstitute——一种较新的mock框架,具有非常有表现力的API(没有lambdas),并具有与Moq和Rhino mock类似的功能。它最近已经达到了1.0版本。
A usage example (from the NSubstitute homepage):
使用示例(来自NSubstitute主页):
//Create:
var calculator = Substitute.For<ICalculator>();
//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
//Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);
//Raise events
calculator.PoweringUp += Raise.Event();
A detailed comparison of the readability of tests using RhinoMocks, Moq and NSubstitute is given here.
本文详细比较了使用RhinoMocks、Moq和NSubstitute进行测试的可读性。
#5
3
stubs looks interesting and I think it may ship with .NET V4. I don’t know witch edition of developer studio you will need to use it. At present you can download it from the above link.
存根看起来很有趣,我认为它可能与。net V4一起发布。我不知道开发人员工作室的女巫版你需要使用它。目前你可以从上面的链接下载。
Stubs is a lightweight framework for test stubs and detours in .NET that is enterily based on delegates, type safe, refactorable and source code generated. Stubs was designed support the Code Contracts runtime writter and provide a minimal overhead to the Pex white box analysis. Stubs may be used on any .NET method, including non-virtual/static methods in sealed types.
存根是. net中测试存根和弯路的轻量级框架,它基于委托、类型安全、可重构和生成的源代码。存根的设计支持代码契约运行时写入,并为Pex白盒分析提供最小的开销。存根可以在任何。net方法中使用,包括密封类型中的非虚拟/静态方法。
(I have posted this as an answser rathern then adding it to the quesion, as I have never used Stabs and only spend a few minutes looking at it)
(我把这个贴在答题卡上,然后把它加到问题中,因为我从来没有用过刀,只花了几分钟看它)
#6
2
RhinoMock is pretty much the state-of-the art mocking framework for .NET. Can't go wrong with it. I guess you can view its "style" as a "Jack of all trades" if you so wish.
RhinoMock几乎是. net最新的模仿框架。不会出错的。如果你愿意,我猜你可以把它的“风格”看成是“万事通”。
From their web site:
从他们的网站:
What does Rhino Mocks offer?
犀牛模拟提供什么?
- Explicit record & replay model for expectations.
- 明确的记录和回放模型。
- Natural Arrange, Act, Assert syntax
- 自然排列、行为、断言语法
- Support for .Net 2.0 and .Net 3.5
- 支持。net 2.0和。net 3.5
- Working with strongly typed mocks.
- 使用强类型模拟。
- Setting actions on methods, return spesific value, or throw an exception.
- 在方法上设置操作、返回spesific值或抛出异常。
- Expectations based on:
- Arguments matching
- 参数匹配
- Constraints matching
- 约束匹配
- Custom callback to verify the expected arguments using your own code
- 自定义回调,使用您自己的代码验证预期的参数
- 基于的期望:参数匹配约束,匹配自定义回调,以使用您自己的代码验证预期的参数
#7
0
I would recommend FakeItEasy. It's a really descriptive mocking framework that allows very easy reading and mocking of interfaces. It has an active open source community and works very well.
我建议FakeItEasy。这是一个描述性的mock框架,允许非常容易地读取和模拟接口。它有一个活跃的开源社区,并且工作得很好。
#8
0
I use Telerik JustMock, it is very professional and easy mock framework with a good document.
我使用Telerik JustMock,它是非常专业和容易的模拟框架,具有良好的文档。
#1
21
So what questions should I by asking about the project and myself to help decide on the best mocking framework to use in a given case?
那么,我应该通过问关于项目和我自己的什么问题来帮助决定在给定的情况下使用什么样的mock框架呢?
The questions you should be asking about the project is: Has the project been developed with the SOLID principles, or not? Is this a project that has loose coupling and high cohesion? Have good OO principles been utilized in building the project? Is a Dependency Injection container being utilized? Has the system been coded in a Design by Contract method (utilizing Interfaces thoroughly)?
关于这个项目,你应该问的问题是:这个项目是否有坚实的原则?这是一个具有松散耦合和高内聚性的项目吗?在构建项目时是否使用了良好的OO原则?是否正在使用依赖注入容器?系统是否用契约方法(充分利用接口)进行设计编码?
If you answer yes to these questions, then you can utilize a mocking framework like RhinoMocks, which is what some would call an "opinionated" framework. RhinoMocks, and some other mocking frameworks, have very strong opinions about how a system should be designed in order for objects to be mocked. A framework like RhinoMocks expects your project to be designed a certain way. Mocking is certainly a lot easier with RhinoMocks when you've built your code the right way (no sealed classes, no statics, heavy use of interfaces, virtual on class methods, etc.)
如果你对这些问题的回答是“是”,那么你可以使用一个像RhinoMocks这样的嘲弄框架,这就是一些人所说的“固执己见”的框架。RhinoMocks和其他一些mock框架对于如何设计一个系统以便对对象进行模拟有着非常强烈的看法。像RhinoMocks这样的框架期望您的项目以某种方式设计。当你以正确的方式构建你的代码(没有封闭的类,没有静态,大量使用接口,虚拟在类方法等等)时,用RhinoMocks来模拟肯定要容易得多。
If you answer no to those questions, or if you're working on a legacy system with a lot of highly coupled classes, then your only choice is going to be TypeMock, which can mock just about anything.
如果您对这些问题的回答是否定的,或者如果您正在处理一个具有大量高耦合类的遗留系统,那么您惟一的选择就是TypeMock,它可以模拟任何事情。
It would also be useful to know why you choose the mocking framework you are currently using and if you are still happy with that choose.
了解为什么选择当前使用的mock框架,以及您是否仍然满意这个选择,也将是非常有用的。
I chose RhinoMocks because at the time (3+ years ago) it was clearly the most mature mocking framework with the most features. I've stayed with it because it has evolved in away that makes my life much easier (the advent of the AutoMocking container being a gigantic step toward efficiency).
我选择RhinoMocks是因为在当时(3年多以前),它显然是最成熟的mock框架,具有最多的特性。我一直坚持使用它,因为它的发展让我的生活变得更容易(自动容器的出现是向效率迈出的巨大一步)。
What I like about RhinoMocks, other than the feature set and ease of use, is that it guides me toward a better design in my code. I am not a perfect programmer, and I am going to make mistakes in design. But tools like RhinoMocks and NHibernate help guide me toward a better design, because when I do make mistakes and create poor design, these tools become painful to work with. NHibernate, for instance, is painful to work with if you have a bad database design. RhinoMocks is very painful to work with if you have a poor class design, aren't using interfaces, aren't using IoC... etc.
我喜欢RhinoMocks,除了它的特性集和易用性,它引导我在我的代码中更好的设计。我不是一个完美的程序员,我将会在设计上犯错误。但是,像RhinoMocks和NHibernate这样的工具可以帮助我实现更好的设计,因为当我犯错误并创建糟糕的设计时,这些工具就会变得难以使用。例如,如果您的数据库设计不好,那么使用NHibernate会很痛苦。如果你的类设计不好,没有使用接口,没有使用IoC,那么使用RhinoMocks是非常痛苦的。等。
I like RhinoMocks because it ultimately helps me be a better developer, and not just because I'm testing my code, but because I'm shaping my code - designing it - in a better manner.
我喜欢RhinoMocks,因为它最终帮助我成为一个更好的开发人员,不仅仅是因为我正在测试我的代码,还因为我正在以更好的方式修改我的代码——设计它。
#2
14
I prefer Moq for .NET 3.5 projects. It is simple to use and, in my experience, it helps to produce clean and clear unit tests.
我更喜欢。net 3.5项目的Moq。它使用起来很简单,而且根据我的经验,它有助于生成清晰的单元测试。
There's no technical reason why you can't use more than one mocking framework on the same project. Sure, it's nice to standardize, but some tests may lend themselves better to some frameworks.
在同一个项目中,不能使用多个mock框架,这是没有技术原因的。当然,标准化很好,但是有些测试可能会更好地应用于某些框架。
#3
10
Disclaimer – I work for Typemock
免责声明:我为排印机工作
I disagree with "Typemock does not drive you to good design". It is you, the developer that drives a good or bad design, not the tool that you use. Get the tool that gets the job done, and make you productive, but the responsibility of good design is your own. If you think that wrapping all kind of abstractions inside your code, just for testing, go for it, but you may not come up with a "good" design. It could be more complex than the design you have right now.
我不同意“类型锁不会让你得到好的设计”。是你自己,开发人员在驱动一个好的或坏的设计,而不是你使用的工具。让工具完成工作,使你富有成效,但是好的设计的责任是你自己的。如果您认为在代码中包装所有类型的抽象,只是为了测试,那么就去做,但是您可能不会想到一个“好的”设计。它可能比你现在的设计还要复杂。
#4
8
What should I consider when choosing a mocking framework for .Net
在为。net选择一个mock框架时,我应该考虑什么
Another aspect to consider, besides the design of the code base that you are testing, is how readable your tests are when using mocks.
另外一个需要考虑的方面是,除了您正在测试的代码库的设计之外,您的测试在使用模拟时是多么的易读。
In this regard, I am putting in a vote for NSubstitute - one of the newer mocking frameworks with a very expressive API (no lambdas) and comparable functionality with Moq and Rhino Mocks. It has recently reached version 1.0.
在这方面,我将投票支持NSubstitute——一种较新的mock框架,具有非常有表现力的API(没有lambdas),并具有与Moq和Rhino mock类似的功能。它最近已经达到了1.0版本。
A usage example (from the NSubstitute homepage):
使用示例(来自NSubstitute主页):
//Create:
var calculator = Substitute.For<ICalculator>();
//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
//Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);
//Raise events
calculator.PoweringUp += Raise.Event();
A detailed comparison of the readability of tests using RhinoMocks, Moq and NSubstitute is given here.
本文详细比较了使用RhinoMocks、Moq和NSubstitute进行测试的可读性。
#5
3
stubs looks interesting and I think it may ship with .NET V4. I don’t know witch edition of developer studio you will need to use it. At present you can download it from the above link.
存根看起来很有趣,我认为它可能与。net V4一起发布。我不知道开发人员工作室的女巫版你需要使用它。目前你可以从上面的链接下载。
Stubs is a lightweight framework for test stubs and detours in .NET that is enterily based on delegates, type safe, refactorable and source code generated. Stubs was designed support the Code Contracts runtime writter and provide a minimal overhead to the Pex white box analysis. Stubs may be used on any .NET method, including non-virtual/static methods in sealed types.
存根是. net中测试存根和弯路的轻量级框架,它基于委托、类型安全、可重构和生成的源代码。存根的设计支持代码契约运行时写入,并为Pex白盒分析提供最小的开销。存根可以在任何。net方法中使用,包括密封类型中的非虚拟/静态方法。
(I have posted this as an answser rathern then adding it to the quesion, as I have never used Stabs and only spend a few minutes looking at it)
(我把这个贴在答题卡上,然后把它加到问题中,因为我从来没有用过刀,只花了几分钟看它)
#6
2
RhinoMock is pretty much the state-of-the art mocking framework for .NET. Can't go wrong with it. I guess you can view its "style" as a "Jack of all trades" if you so wish.
RhinoMock几乎是. net最新的模仿框架。不会出错的。如果你愿意,我猜你可以把它的“风格”看成是“万事通”。
From their web site:
从他们的网站:
What does Rhino Mocks offer?
犀牛模拟提供什么?
- Explicit record & replay model for expectations.
- 明确的记录和回放模型。
- Natural Arrange, Act, Assert syntax
- 自然排列、行为、断言语法
- Support for .Net 2.0 and .Net 3.5
- 支持。net 2.0和。net 3.5
- Working with strongly typed mocks.
- 使用强类型模拟。
- Setting actions on methods, return spesific value, or throw an exception.
- 在方法上设置操作、返回spesific值或抛出异常。
- Expectations based on:
- Arguments matching
- 参数匹配
- Constraints matching
- 约束匹配
- Custom callback to verify the expected arguments using your own code
- 自定义回调,使用您自己的代码验证预期的参数
- 基于的期望:参数匹配约束,匹配自定义回调,以使用您自己的代码验证预期的参数
#7
0
I would recommend FakeItEasy. It's a really descriptive mocking framework that allows very easy reading and mocking of interfaces. It has an active open source community and works very well.
我建议FakeItEasy。这是一个描述性的mock框架,允许非常容易地读取和模拟接口。它有一个活跃的开源社区,并且工作得很好。
#8
0
I use Telerik JustMock, it is very professional and easy mock framework with a good document.
我使用Telerik JustMock,它是非常专业和容易的模拟框架,具有良好的文档。