4
votes

I am trying to write a test that covers my error handling in a particular class. This class is listening for an Error event with the following signature:

OnError(int ErrorNumber, string ErrorText, ref bool retry)

The problem is with the ref bool variable at the end. I am using Rhino Mocks to create a mock interface for testing and when I try to raise the error using the following:

bool retry = false;
AdapterMock.Raise(x => x.Error += null, 0, "0", ref retry);

It won't even compile, telling me it can't convert from ref bool to Object.

If I change the signature to:

bool retry = false;
AdapterMock.Raise(x => x.Error += null, 0, "0", retry);

I compiles fine but the test fails with System.InvalidOperationException : Parameter #3 is System.Boolean but should be System.Boolean&

I'm pulling my hair out on this one, how do I properly raise this event in my mock?

1
Any possibility of re-writing the OnError delegate to the .NET-standards for arguments -- i.e. (object sender, OnErrorEventArgs args)? - PatrickSteele

1 Answers

0
votes

Try:

AdapterMock.Raise( x=> x.Error += null, Arg<int>.Is.Equal(0), Arg<string>.Is.Equal("0"), Arg.Ref(ref Is.Equal(retry)));