【发布时间】:2014-03-25 13:44:29
【问题描述】:
我是 C# Moq 的新手(过去使用过 Rhino Mochs)并且需要测试对同一方法的一系列调用。我发现这个很酷的解决方案可以测试一系列返回值:
http://haacked.com/archive/2009/09/29/moq-sequences.aspx/
public static class MoqExtensions
{
public static void ReturnsInOrder<T, TResult>(this ISetup<T, TResult> setup,
params TResult[] results) where T : class {
setup.Returns(new Queue<TResult>(results).Dequeue);
}
}
我需要做的是在对同一方法的一系列调用中测试作为方法参数发送的值(而不是它返回的值)。
粗略的轮廓...
var expression = new MyExpressionThing();
processor.Setup(x => x.Execute(expected1)).Verifiable();
processor.Setup(x => x.Execute(expected2)).Verifiable();
processor.Setup(x => x.Execute(expected3)).Verifiable();
expression.ExecuteWith(processor.Object);
processor.Verify();
这是我尝试过的,但我遇到了异常:
“System.ArgumentException : 无效的回调。在带参数的方法上设置 (String,Object[]) 无法调用带参数的回调 (String)。”
// Arrange
var processor = new Mock<IMigrationProcessor>();
IList<string> calls = new List<string>();
processor.Setup(p => p.Execute(It.IsAny<string>()))
.Callback<string>(s => calls.Add(s));
// Act
var expr = new ExecuteScriptsInDirectoryExpression { SqlScriptDirectory = @"SQL2\1_Pre" };
expr.ExecuteWith(processor.Object);
// Assert
calls.ToArray().ShouldBe(new[]
{ "DELETE FROM PRE1A", "DELETE FROM PRE1B", "INSERT INTO PRE2\r\nLINE2" });
看起来我正在使用起订量“入门”示例中的样板代码:
此链接讨论此异常并链接到触发它的最小起订量代码。
http://dailydevscoveries.blogspot.com.au/2011/04/invalid-callback-setup-on-method-with.html
【问题讨论】: