[英]JustMock - check value of passed method argument
我使用JustMock框架并具有以下断言:
Mock.Assert(() => activityListenerMock.PeriodPassed(
Arg.Matches<Period>(e => e.Length == expectedLength)));
它失败了,含有神秘的信息:
Occurrence expectation failed. Expected at least 1 call. Calls so far: 0
我怎样才能得到更好的信息。 我想知道它被称为什么价值。
方法实际上被调用但是有错误的参数,因为当我将断言更改为跟随它传递时:
Mock.Assert(() => activityListenerMock.PeriodPassed(
Arg.IsAny<Period>()));
查看传递给PeriodPassed
参数的一种方法是使用JustMock的DebugView
放置DebugView.IsTraceEnabled = true;
在测试开始时将DebugView.CurrentState
添加到手表中。 接近最后,你会看到一些调整: Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)
Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)
期间值将显示在“调用”列表中。
另一种方法是将匹配器提取到一个单独的lambda中并使用断点: Predicate<Period> matcher = e => e.Length == expectedLength; Mock.Assert(() => activityListenerMock.PeriodPassed( Arg.Matches<Period>(e => matcher(e))));
Predicate<Period> matcher = e => e.Length == expectedLength; Mock.Assert(() => activityListenerMock.PeriodPassed( Arg.Matches<Period>(e => matcher(e))));
现在,您可以在谓词中放置断点并检查e
参数的值。 这是有效的,因为现在谓词不是表达式而是实际函数,所以现在你可以调试它。
就像Stefan Dragnev写的那样。 我使用他的想法,然后添加逻辑来验证输入。 如果不是预期值,则调用Assert.Fail()。 不确定是否有更好的方法,但这有效:
Mock.Arrange(() => _uowMock.Class.Add(
Arg.Matches<ModelClass>(x => (CheckArgs(x, updated)))))
.DoNothing().Occurs(3);
....
protected static bool CheckArgs(ModelClass x, int y)
{
if (x.val != y)
{
Assert.Fail("Houston we have a problem");
}
return true;
}
添加额外的安排之前也为我工作,但它是非常hacky:
Mock.Arrange(() => activityListenerMock.PeriodPassed(Arg.IsAny<Period>())).
DoInstead((Period p) => Console.WriteLine("Actual " + p.Length+" expected "+expectedLength));
今天陷入同样的困境,并开始扩展Krzysztof的想法,扩展一些。 它虽然粗糙但功能齐全。
public static class JustMockExtensions {
public static FuncExpectation<T> PrintParams<T, T1>(this FuncExpectation<T> mock) {
return mock.DoInstead<T1, T>((arg1, arg2) => {
string message = string.Empty;
message += Process(arg1);
message += Process(arg2);
Console.WriteLine(message);
});
}
private static string Process<T>(T obj) {
if (typeof(T).IsEnum) {
return Enum.GetName(typeof(T), obj);
}
return obj.ToString();
}
}
到目前为止,以这种方式使用它允许它在正常流程中进行管道传输。
Mock.Arrange(() => foo.bar(Arg.IsAny<Widget>(), Arg.IsAny<WidgetTypeEnum>()))
.PrintParams<Widget, WidgetTypeEnum>()
.MustBeCalled();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.