我正在尝试测试类似于以下示例的类:
public class Service : IService
{
public string A(string input)
{
int attemptCount = 5;
while (attemptCount > 0)
{
try
{
return TryA(input);
}
catch (ArgumentOutOfRangeException)
{
attemptCount--;
if (attemptCount == 0)
{
throw;
}
// Attempt 5 more times
Thread.Sleep(1000);
}
}
throw new ArgumentOutOfRangeException();
}
public string TryA(string input)
{
// try actions, if fail will throw ArgumentOutOfRangeException
}
}
[TestMethod]
public void Makes_5_Attempts()
{
// Arrange
var _service = MockRepository.GeneratePartialMock<Service>();
_service.Expect(x=>x.TryA(Arg<string>.Is.Anything)).IgnoreArguments().Throw(new ArgumentOutOfRangeException());
// Act
Action act = () => _service.A("");
// Assert
// Assert TryA is attempted to be called 5 times
_service.AssertWasCalled(x => x.TryA(Arg<string>.Is.Anything), opt => opt.Repeat.Times(5));
// Assert the Exception is eventually thrown
act.ShouldThrow<ArgumentOutOfRangeException>();
}
部分嘲讽似乎无法接受我的期望。当我运行测试时,我收到有关输入的错误。当我调试时,我看到正在执行该方法的实际实现而不是预期。
我做这个测试正确吗?根据文档(http://ayende.com/wiki/Rhino%20Mocks%20Partial%20Mocks.ashx):“除非您为该方法定义期望,否则部分模拟将调用该类上定义的方法。如果您有定义了一个期望,它将为此使用正常规则。”