假货比假货好吗?
我偶然发现了这个开源项目Fake It Easy,我不得不承认,它看起来非常有趣,但我有疑问,FIE 假货和 Moq Mocks 有什么区别?有没有哪一种更适合特定用途?
编辑:
这个新框架有什么特点使它比起订量更好?
I stumbled upon this open source project Fake It Easy, and I have to admit, it looks very interesting, however I have my doubts, what are the difference between FIE fakes and say Moq Mocks? Is any one better for particular uses?
EDIT:
What is it about this new framework that would make it better than say Moq?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
需要明确的是,我创建了 FakeItEasy,所以我绝对不会说一个框架是否比另一个更好,我能做的就是指出一些差异并激发我创建 FakeItEasy 的原因。从功能上来说,Moq 和 FakeItEasy 之间没有重大差异。
FakeItEasy 没有“可验证”或“期望”,它有断言,但是,这些断言总是在测试的最后明确说明,我相信这使得测试更容易阅读和理解。它还可以帮助初学者避免多个断言(他们会对许多调用或模拟对象设置期望)。
我之前使用过Rhino Mocks,并且非常喜欢它,尤其是在引入AAA语法之后,我确实更喜欢Moq的流畅API。我不喜欢 Moq 的是“模拟对象”,你必须在任何地方使用 mock.Object,我更喜欢使用“自然”模拟的 Rhino 方法。每个实例看起来和感觉都像伪造类型的正常实例。我想要两全其美,而且我想看看当我有完全自由的双手时我可以用语法做些什么。就我个人而言(显然),我认为我创造的东西是两个世界最好的东西的完美结合,但当你站在巨人的肩膀上时,这很容易。
正如这里提到的,主要区别之一在于术语,FakeItEasy 最初是为了向初学者介绍 TDD 和模拟而创建的,并且必须预先担心模拟和存根之间的差异(在 Rhino 中必须这样做)我认为不是很有用。
我已经将很多注意力放在异常消息上,只需查看异常消息就应该很容易判断测试中出现了什么问题。
FakeItEasy 具有其他框架所没有的一些可扩展性功能,但这些功能尚未得到很好的记录。
FakeItEasy(希望)在模拟具有构造函数参数的类方面稍强一些,因为它具有解析要使用的虚拟值的机制。您甚至可以通过在测试项目中实现 DummyDefinition(Of T) 类来指定您自己的虚拟值定义,FakeItEasy 将自动选取该类。
语法是一个明显的区别,哪个更好很大程度上取决于品味。
我确信还有很多其他差异我现在已经忘记了(公平地说,我自己从未在生产中使用过 Moq,所以我对它的了解有限),但我确实认为这些是最重要的差异。
To be clear, I created FakeItEasy so I'll definitely not say whether one framework is better than the other, what I can do is point out some differences and motivate why I created FakeItEasy. Functionally there are no major differences between Moq and FakeItEasy.
FakeItEasy has no "Verifiable" or "Expectations" it has assertions however, these are always explicitly stated at the very end of a test, I believe this makes tests easier to read and understand. It also helps beginners to avoid multiple asserts (where they would set expectations on many calls or mock objects).
I used Rhino Mocks before and I quite liked it, especially after the AAA-syntax was introduced I did like the fluent API of Moq better though. What I didn't like with Moq was the "mock object" where you have to use mock.Object everywhere, I like the Rhino-approach with "natural" mocks better. Every instance looks and feels like a normal instance of the faked type. I wanted the best of both worlds and also I wanted to see what I could do with the syntax when I had absolutely free hands. Personally I (obviously) think I created something that is a good mix with the best from both world, but that's quite easy when you're standing on the shoulders of giants.
As has been mentioned here one of the main differences is in the terminology, FakeItEasy was first created to introduce TDD and mocking to beginners and having to worry about the differences between mocks and stubs up front (the way you would have to in Rhino) is not very useful in my opinion.
I've put a lot of focus into the exception messages, it should be very easy to tell what whent wrong in a test just looking at an exception message.
FakeItEasy has some extensibility features that the other frameworks don't have but these aren't very well documented yet.
FakeItEasy is (hopefully) a little stronger in mocking classes that has constructor arguments since it has a mechanism for resolving dummy-values to use. You can even specify your own dummy value definitions by implementing a DummyDefinition(Of T) class within your test project, this will automatically be picked up by FakeItEasy.
The syntax is an obvious difference, which one is better is largely a matter of taste.
I'm sure there are lots of other differences that I forget about now (and to be fair I have never used Moq in production myself so my knowledge of it is limited), I do think these are the most important differences though.
测试中使用的术语可能有点令人困惑。解释不同概念之间差异的最佳来源是 Martin Fowler 的 Mocks Aren't Stubs。总之,fake 是一个描述存根和模拟的通用术语。
The terminology used in testing can be slightly confusing. The best source explaining the difference between different concepts is Mocks Aren't Stubs by Martin Fowler. In summary, fake is a generic term that describes both stubs and mocks.
模拟中的术语可能会令人困惑 - 有时甚至相当不直观。
因此,许多人提出了一个更简单的新术语,其中只有fakes、mocks和stubs。
Fake 是所有可能类型的测试替身的通用术语,无论它们来自何处以及如何使用。
除此之外,假货仅在一个维度上进行区分:是否影响测试结果;是否影响测试结果;是否影响测试结果。或者,换句话说:是否必须为假对象设置返回值(这些值在测试执行期间以某种方式使用),或者它是一个“沉默”对象,仅用于满足某些依赖关系。< br>
Stub 它就是那个“沉默”对象。
Mock 它积极参与测试执行
除此之外,没有进一步的区别 - 这肯定有其历史优点,但现在很大程度上是相反的 -直观且学术性强,并且它混淆了测试驱动开发的真正重要概念。
关于Moq和FakeItEasy之间的比较:从概念的角度来看,这两个框架基本相同 - 差异只是在 API 和术语中...
Thomas
The terminology in mocking can be confusing - and sometimes is quite unintuitive.
Therefore, many people proposed a simpler, new terminology, where you have only fakes, mocks, and stubs.
Fake is the generic term for all possible kinds of test doubles, no matter where they come from and how they are used.
Beyond that, fakes are distinguished only along one single dimension: whether they influence test outcome or not; or, in other words: whether you have to set up return values for the fake, which are somehow used during test execution, or it is a 'silent' object which only serves to fulfill some dependency.
Stub it is that 'silent' object.
Mock it is actively participates in test execution
Beyond that, there's no further distinction - which surely has its historical merits, but is now largely counter-intuitive and academical, and it's kind of obfuscating really important concepts of Test-driven development.
Concerning the comparison between Moq and FakeItEasy: the two frameworks are largely the same from a conceptual point of view - the differences are only in the API and in the terminology...
Thomas
从我的角度来看,Fake 不会取消 Moc,例如,我使用 Dev Magic Fake 来伪造 DAL 和业务层,同时我在 MVC 中使用 Mock 来获取 HTTPContext
在前面的代码中,Dev Magic Fake 将保存 ProductTypeForm 并检索来自 Dev Magic Fake 的 VendorForm 并将其链接到 ProductTypeForm,此保存操作可以是永久性的
有关 Dev Magic Fake 的更多信息,请在 CodePlex 上查看:
http://devmagicfake.codeplex.com
测试这个方法我们必须模拟 HTTP 上下文
所以我使用 fake并嘲笑
From my point of view Fake will not cancels the Moc for example I use Dev Magic Fake to fake DAL and Business layer and in the same time I use Mock in MVC to for HTTPContext
In the previous code Dev Magic Fake will save the ProductTypeForm and retrieve the VendorForm from Dev Magic Fake and link it to ProductTypeForm, this save operation can be permanent
For more inforamtion about Dev Magic Fake see it on CodePlex:
http://devmagicfake.codeplex.com
Te test this method we Have to Mock the HTTP context
So I work with fake and mock