【问题标题】:How to stub a method returning void with ref arguments with Rhino如何使用 Rhino 使用 ref 参数对返回 void 的方法进行存根
【发布时间】:2012-03-08 00:19:51
【问题描述】:

如果方法返回 void,我找不到使用 ref 参数存根方法的方法,如下例所示:

public interface Interface1 {
    void Method1(ref int i);
}

public class Class1 {
    static public void Main() {
    MockRepository mockRepository = new MockRepository();
    Interface1 interface1 = mockRepository.Stub<Interface1>();
    int i = 1;
    //SetupResult.For(interface1.Method1(ref i)).OutRef(1);  Can't compile
    interface1.Method1(ref i);
    LastCall.Repeat.Any();
    mockRepository.ReplayAll();
    int j = 0;
    interface1.Method1(ref j);
    if(j == 1) Console.WriteLine("OK");
}

你有什么想法吗?

谢谢, 斯泰尼奥

【问题讨论】:

    标签: c# unit-testing rhino-mocks stubbing


    【解决方案1】:

    Rhino Mocks 3.5 有一个新的约束接口,取代了 .OutRef() 等。见documentation:

    Interface1 interface1 = MockRepository.GenerateStub<Interface1>();
    int i = 1;
    interface1.Stub(x => x.Method1(ref Arg<int>.Ref(Is.Anything(), i).Dummy);
    int j = 0;
    interface1.Method1(ref j);
    if (j == 1) Console.WriteLine("OK");
    

    【讨论】:

    • 无论如何,对于在其 ref/out 参数中返回某些内容的方法来说,这是相当多的语法糖。我想知道声明式方法是否会更好,不是吗?
    • 我可能不会改变它,但它不是我的图书馆。 :)
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 2011-12-05
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多