【问题标题】:Rhino mocks ordered reply, throw exception problemRhino 模拟命令回复,抛出异常问题
【发布时间】:2008-09-19 16:40:19
【问题描述】:

如果我的代码中有异常,我正在尝试实现一些重试逻辑。我已经编写了代码,现在我正在尝试让 Rhino Mocks 来模拟场景。代码要点如下:

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

似乎发生的情况是每次都抛出异常,而不仅仅是一次。我应该将设置更改为什么?

【问题讨论】:

    标签: exception mocking rhino-mocks


    【解决方案1】:

    您需要使用 Expect.Call 而不是 SetupResult:

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

    Rhino.Mocks wiki 说,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多