I am trying to figure out how to create a mock using rhino mocks that will call the original method the first time a specific method (BasicPublish) is called, throw an exception the second time, then call the original method for all remaining calls to the method.
The original method signature looks like this:
public virtual void BasicPublish(
string exchange, string routingKey, IBasicProperties basicProperties, byte[] body)
Is this possible?
Here is what I have tried that I thought would work. My test is now calling the orginal method. But after setting a break point in my method and debugging my calls to BasicPublish, all the parameters are being passed through as null. Am I using the "CallOriginalMethod(OriginalCallOptions.CreateExpectation)" incorrectly?
mockModel.Stub(a => a.BasicPublish(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<IBasicProperties>.Is.Anything, Arg<byte[]>.Is.Anything)).CallOriginalMethod(OriginalCallOptions.NoExpectation).Repeat.Once();
mockModel.Stub(a => a.BasicPublish(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<IBasicProperties>.Is.Anything, Arg<byte[]>.Is.Anything)).Throw(new DivideByZeroException()).Repeat.Once();
mockModel.Stub(a => a.BasicPublish(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<IBasicProperties>.Is.Anything, Arg<byte[]>.Is.Anything)).CallOriginalMethod(OriginalCallOptions.NoExpectation).Repeat.Any();
Additional context: This is to be used in a unit test (using nunit) to test the behavior of a custom Log4Net Rabbit MQ appender. The test case is as follows: given a live connection to a queue, when the connection becomes faulty (simulated by an exception on a call to BasicPublish(...)), then that specific log message is ignored and following log messages are processed normally
I wasn't able to find a way to do this with Rhino but realized I could just create a fake myself that would behave the way I expected. This solution turned out to be very readable with proper naming conventions.