简体   繁体   English

与 Rhino Mocks 一起使用的最佳样式/语法是什么?

[英]What is the best style/syntax to use with Rhino Mocks?

Multiple approaches exist to write your unit tests when using Rhino Mocks:使用 Rhino Mocks 时,存在多种编写单元测试的方法:

  • The Standard Syntax标准语法
  • Record/Replay Syntax记录/重放语法
  • The Fluent Syntax流畅的语法

What is the ideal and most frictionless way?什么是理想和最无摩擦的方式?

Arrange, act, assert. 安排,行动,主张。 Tho, I use MoQ and prefer the Arrange, Assert, Act, Verify. 我使用起订量,并且更喜欢安排,声明,执行和验证。 I like to set up everything before I act, rather than do the heavy lifting at the end. 我喜欢在行动之前先设置好一切,而不是最后做繁重的工作。

For .NET 2.0, I recommend the record/playback model.对于 .NET 2.0,我推荐记录/回放模型。 We like this because it separates clearly your expectations from your verifications.我们喜欢这个,因为它清楚地将您的期望与您的验证区分开来。

using(mocks.Record())
{
    Expect.Call(foo.Bar());
}
using(mocks.Playback())
{
    MakeItAllHappen();
}

If you're using .NET 3.5 and C# 3, then I'd recommend the fluent syntax.如果您使用 .NET 3.5 和 C# 3,那么我建议使用流畅的语法。

Interesting question!有趣的问题! My own preference is the for the reflection-based syntax (what I guess you mean by the Standard Syntax).我自己的偏好是基于反射的语法(我猜你的意思是标准语法)。 I would argue that this is the most frictionless, as it does not add much extra code: you reference the stubs directly on your interfaces as though they were properly implemented.我认为这是最无障碍的,因为它不会添加太多额外的代码:您直接在接口上引用存根,就好像它们已正确实现一样。

I do also quite like the Fluent syntax, although this is quite cumbersome.我也很喜欢 Fluent 语法,虽然这很麻烦。 The Record/Replay syntax is as cumbersome as the Fluent syntax (if not more so, seemingly), but less intuitive (to me at least). Record/Replay 语法和 Fluent 语法一样麻烦(如果不是更严重的话,看起来),但不那么直观(至少对我来说)。 I've only used NMock2, so the Record/Replay syntax is a bit alien to me, whilst the Fluent syntax is quite familiar.我只用过 NMock2,所以 Record/Replay 语法对我来说有点陌生,而 Fluent 语法很熟悉。

However, as this post suggests, if you prefer separating your expectations from your verifications/assertions, you should opt for the Fluent syntax.但是,正如这篇文章所建议的,如果您更喜欢将您的期望与您的验证/断言分开,您应该选择 Fluent 语法。 It's all a matter of style and personal preference, ultimately :-)这完全取决于风格和个人喜好,最终:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM