zoukankan      html  css  js  c++  java
  • Rhino Mocks Setup Result

    Sometimes you have a method on your mocked object for which you don't care how or if it was called, but you may want to set a return value (or an exception to be thrown) in case it is called. For example, you may have some interaction that you've already verified, or you are testing some other class and just need to get the right value from a method any time it is called. To do this in Rhino Mocks, use SetupResult.For(). Here is the code:
    Test
    public void SetupResultUsingOrdered()
    {
       SetupResult.For(demo.Prop).Return("Ayende");
       using(mocks.Ordered())
       {
           demo.VoidNoArgs();
           LastCall.On(demo).Repeat.Twice();
       }
       mocks.ReplayAll();
       demo.VoidNoArgs();
       for (int i = 0; i < 30; i++)
       {
           Assert.AreEqual("Ayende",demo.Prop);      
       }
       demo.VoidNoArgs();
    } 
    
    When we use SetupResult.For() we tell Rhino Mocks: "I don't care about this method, it needs to do X, so just do it, but otherwise ignore it." Using SetupResult.For() completely bypasses the expectations model in Rhino Mocks. In the above example, we define demo.Prop to return "Ayende", and we can call it no matter what the currently expected method is.

    What about methods returning void?

    You would use LastCall.Repeat.Any(), which has identical semantics (ignore ordering, etc).

    The opposite of SetupResult.For() is to specify that this method should never be called (useful if you're using dynamic mocks), which is done like this:
    Expect.Call(view.Ask(null,null)).IgnoreArguments().
       .Repeat.Never();
    
    This has the same semantics as ExpectNoCall() in NMock and this tells the framework that any call to this method is an error. Notice that I still call IgnoreArguments(), since otherwise the expectation would be specifically that this method never called with null as both parameters. Like SetupResult.For() and Repeat.Any(), using Repeat.Never() transcends ordering.

    Note: If you want to have a method that can repeat any number of time, but still obeys ordering, you can use: LastCall.On().Repeat.Times(0,int.MaxValue). This does obey all of the normal rules.

  • 相关阅读:
    XSS相关有效载荷及绕道的备忘录(下)| 文末有打包好的负载
    基于windows 10打造的kali工具集
    XSS挑战之旅---游戏通关攻略
    SQLi_Labs通关文档【1-65关】
    Linux 安装 lanmp
    linux 下的init 0,1,2,3,4,5,6知识介绍
    WebGL2系列之多采样渲染缓冲对象
    FireFox下Canvas使用图像合成绘制SVG的Bug
    WebGL 着色器偏导数dFdx和dFdy介绍
    JavaScript 一元正号运算符
  • 原文地址:https://www.cnblogs.com/hyl8218/p/2063428.html
Copyright © 2011-2022 走看看