views:

916

answers:

1

I'm trying to implement some retry logic if there is an exception in my code. I've written the code and now I'm trying to get Rhino Mocks to simulate the scenario. The jist of the code is the following:

class Program
    {
        static void Main(string[] args)
        {
            MockRepository repo = new MockRepository();
            IA provider = repo.CreateMock<IA>();

            using (repo.Record()) 
            {
                SetupResult.For(provider.Execute(23))
                           .IgnoreArguments()
                           .Throw(new ApplicationException("Dummy exception"));

                SetupResult.For(provider.Execute(23))
                           .IgnoreArguments()
                           .Return("result");
            }

            repo.ReplayAll();

            B retryLogic = new B { Provider = provider };
            retryLogic.RetryTestFunction();
            repo.VerifyAll();
        }
    }

    public interface IA
    {
        string Execute(int val);
    }

    public class B
    {
        public IA Provider { get; set; }

        public void RetryTestFunction()
        {
            string result = null;
            //simplified retry logic
            try
            {
                result = Provider.Execute(23);
            }
            catch (Exception e)
            {
                result = Provider.Execute(23);
            }
        }
    }

What seems to happen is that the exception gets thrown everytime instead of just once. What should I change the setup to be?

+2  A: 

You need to use Expect.Call instead of SetupResult:

     using (repo.Record())
 {
  Expect.Call(provider.Execute(23))
       .IgnoreArguments()
       .Throw(new ApplicationException("Dummy exception"));

  Expect.Call(provider.Execute(23))
       .IgnoreArguments()
       .Return("result");
 }

The Rhino.Mocks wiki says,

Using SetupResult.For() completely bypasses the expectations model in Rhino Mocks

Joseph Anderson