Search code examples
c#unit-testingrhino-mocksstubbing

How to stub a method returning void with ref arguments with Rhino


I can't find a way to stub methods with ref arguments if they return void, as in the following example:

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");
}

Do you have any idea?

Thanks, Stenio


Solution

  • Rhino Mocks 3.5 has a new interface for constraints, replacing .OutRef() and others. See the 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");