如果我的代码中有异常,我正在尝试实现一些重试逻辑。我已经编写了代码,现在我正在尝试让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);
}
}
}
似乎发生的事情是每次抛出异常而不是一次抛出异常。我应该将设置更改为什么?
答案 0 :(得分:2)
您需要使用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维基说,
Using SetupResult.For() completely bypasses the expectations model in Rhino Mocks