Rhino 模拟 AAA 快速入门?

发布于 2024-08-18 21:52:26 字数 226 浏览 9 评论 0原文

我一直在寻找一些有关使用 Rhino Mocks 3.5+ 和 AAA 语法的不错的信息。我发现很多博客混合了新旧内容,这似乎使得弄清楚如何使用它变得更加困难。

如果有一个像早期版本那样的 Rhino Mocks AAA 备忘单,那就太好了。您是否需要了解有关旧版本 Rhino 的所有信息才能实际使用新版本?我确信如果我是专家,我会喜欢 Rhino 的所有功能,但现在我只是沉浸在信息中。任何指示或良好的链接将不胜感激!

I've been looking around for some decent information on using Rhino Mocks 3.5+ with the AAA syntax. I find a lot of blogs that have a mix of things from the old and new which seem to make it more difficult to figure out how to use it.

Would would be great would be if there were a Rhino Mocks AAA Cheat Sheet like was done for an earlier version. Is it required that you know everything about the older versions of Rhino to actually use the newer version? I'm sure if I were an expert that I would love all the capabilities in Rhino, but for now I'm just swimming in information. Any pointers or good links would be totally appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2024-08-25 21:52:26

我写了一个 Rhino Mocks Arrange / Act / Assert (AAA) Syntax Quick参考。它包含从 Ayende 网站 收集的有关此语法样式的浓缩信息,以及其他几个博客。

I have written a Rhino Mocks Arrange / Act / Assert (AAA) Syntax Quick Reference. It contains condensed information about this syntax style collected from Ayende's web site and several other blogs.

若能看破又如何 2024-08-25 21:52:26

我假设您熟悉官方文档,我认为这相当不错。
我的建议是尝试使用Rhino,当你遇到一些更具体的问题时,在SO或其他地方寻找解决方案。我认为没有针对 Rhino 模拟的全面备忘单。我想你会更幸运地问“我怎样才能使用Rhino Mocks 做到这一点和那个”

编辑:
那么,当目标为 AAA 时,您不需要使用录制/播放。
AAA涉及三个步骤:

  • Arrange,即设置
    例如,模拟类的行为

IOmicronDll mockWrapper = MockRepository.GenerateMock<IOmicronDll>();
mockWrapper.Expect(wrapper => wrapper.Lock(1, ref errors)).OutRef(string.Empty).Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec(1, "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec("1", "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
Microsoft.Practices.Unity.UnityContainer c = new Microsoft.Practices.Unity.UnityContainer();
c.RegisterInstance<IOmicronDll>(mockWrapper);
  • Act,代表执行测试

    public Omicron(int deviceID)
    {
        尝试
        {
            if (g_Omicron == null)
                g_Omicron = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance();
            m_UniqueIdentifier = Guid.NewGuid();
            m_Logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance();
            m_ID = 设备ID;
            获取配置();
            g_InstancesCount++;
            m_PollThread = new Thread(new ThreadStart(DoPoll));
            m_PollThread.Start();
        }
    
  • Assert,代表veryfing
    结果

Assert.AreEqual("CMC 56", omicron.Type);
mockWrapper.AssertWasCalled(wrapper => wrapper.Release(), options => options.Repeat.AtLeastOnce());

也许上面的例子不是最好的,但可能会让你找到正确的方向。

I assume that you're familiar with official documentation, which is quite good in my opinion.
My suggestion is to try to use Rhino, and when you encounter some more specific problem search for solution wither on SO or somewhere else. I don't think that there is a comprehensive cheat sheet for Rhino mocks. I guess you'll have more luck asking "How can I do this and that using Rhino Mocks"

Edit:
Well, you don't need to use Record/Playback when targeting AAA.
There are three steps involved in AAA:

  • Arrange, which stands for setting up
    the behavior of mocked class, for example

.

IOmicronDll mockWrapper = MockRepository.GenerateMock<IOmicronDll>();
mockWrapper.Expect(wrapper => wrapper.Lock(1, ref errors)).OutRef(string.Empty).Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec(1, "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec("1", "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
Microsoft.Practices.Unity.UnityContainer c = new Microsoft.Practices.Unity.UnityContainer();
c.RegisterInstance<IOmicronDll>(mockWrapper);
  • Act, which stands for executing the tests

    public Omicron(int deviceID)
    {
        try
        {
            if (g_Omicron == null)
                g_Omicron = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<CMEngineWrapper.IOmicronDll>();
            m_UniqueIdentifier = Guid.NewGuid();
            m_Logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<AdvAdmittance.Framework.ILogger>();
            m_ID = deviceID;
            GetConfiguration();
            g_InstancesCount++;
            m_PollThread = new Thread(new ThreadStart(DoPoll));
            m_PollThread.Start();
        }
    
  • And Assert, which stands for veryfing
    the results

.

Assert.AreEqual("CMC 56", omicron.Type);
mockWrapper.AssertWasCalled(wrapper => wrapper.Release(), options => options.Repeat.AtLeastOnce());

Perhaps the above examples aren't the best, but might get you into right direction.

指尖上得阳光 2024-08-25 21:52:26

首先确保您了解 AAA 中每个 A 的含义。您可能知道,但为了答案的完整性,我将包括我的工作定义:

  • 排列是我设置输入、模拟/存根、具有被测试方法的对象
  • Act的 地方> 是我调用被测方法的地方
  • Assert 是我验证事情发生或没有按照预期发生的地方

我喜欢在测试代码中添加注释,以提醒我考虑每一件事情。一个例子可能有助于澄清:假设我有一个服务层类,它使用两个提供者层类,一个来自“旧”系统,一个来自“新”系统;我正在测试将旧事物复制到新系统的方法对于找到的每个旧事物都会调用一次“CreateThing”方法。

[Test]
public void Should_create_new_Thing_for_each_old_Thing()
{
  // -----
  // arrange

  // hardcode results from old system provider
  List<Thing> oldThings = new List<Thing> { ... };

  // old system provider
  var oldProvider = MockRepository.GenerateStub<IOldSystemProvider>();
  oldProvider.Stub(m=>m.GetThings()).Return(oldThings);

  // new system provider
  var newProvider = MockRepository.GenerateStub<INewSystemProvider>();

  // service object
  var svc = new MyService(oldProvider, newProvider);

  //-----------
  // act
  var result = svc.CopyThings();

  //------------
  // assert
  oldThings.ForEach(thing => 
                    newProvider.AssertWasCalled(prov => prov.CreateThing(thing)));
}

First make sure you know what you mean for each A in AAA. You may know but I'll include my working definitions for completeness of the answer:

  • Arrange is where I set up inputs, mocks/stubs, the object with the method under test
  • Act is where I call the method under test
  • Assert is where I verify things occurred or didn't according to expectation

I like to put comments in my test code to remind me to think about each of those things. An example may help clarify: Suppose I have a service layer class which uses two provider layer classes, one from an "old" system and one from a "new" system; I am testing that the method which copies old things to the new system calls the "CreateThing" method one time for each old thing found.

[Test]
public void Should_create_new_Thing_for_each_old_Thing()
{
  // -----
  // arrange

  // hardcode results from old system provider
  List<Thing> oldThings = new List<Thing> { ... };

  // old system provider
  var oldProvider = MockRepository.GenerateStub<IOldSystemProvider>();
  oldProvider.Stub(m=>m.GetThings()).Return(oldThings);

  // new system provider
  var newProvider = MockRepository.GenerateStub<INewSystemProvider>();

  // service object
  var svc = new MyService(oldProvider, newProvider);

  //-----------
  // act
  var result = svc.CopyThings();

  //------------
  // assert
  oldThings.ForEach(thing => 
                    newProvider.AssertWasCalled(prov => prov.CreateThing(thing)));
}
长梦不多时 2024-08-25 21:52:26

您可能想从Ayende 在 AAA 上的原始帖子开始,然后看看 Ben Hall 关于 AAA 的精彩博客文章。比您可以使用其他答案中提到的所有完整参考文献......

You'd probably want to start at Ayende's original post on AAA, and than take a look at Ben Hall's nice blog post about AAA. Than you could use all the complete references mentioned in other answers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文