[英]JustMock - check value of passed method argument
I use JustMock framework and have the following assertion: 我使用JustMock框架并具有以下断言:
Mock.Assert(() => activityListenerMock.PeriodPassed(
Arg.Matches<Period>(e => e.Length == expectedLength)));
It fails with cryptic message: 它失败了,含有神秘的信息:
Occurrence expectation failed. Expected at least 1 call. Calls so far: 0
How can I get better message. 我怎样才能得到更好的信息。 I want to know with what value it was called.
我想知道它被称为什么价值。
Method is actually called but with wrong argument because when I change assertion to following it passes: 方法实际上被调用但是有错误的参数,因为当我将断言更改为跟随它传递时:
Mock.Assert(() => activityListenerMock.PeriodPassed(
Arg.IsAny<Period>()));
One way to see what argument was passed to PeriodPassed
is to use JustMock's DebugView 查看传递给
PeriodPassed
参数的一种方法是使用JustMock的DebugView
Place DebugView.IsTraceEnabled = true;
放置
DebugView.IsTraceEnabled = true;
at the beginning of the test and add DebugView.CurrentState
to the watch. 在测试开始时将
DebugView.CurrentState
添加到手表中。 Towards the end you will see something to the tune of this: Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)
接近最后,你会看到一些调整:
Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)
Invocations: (ByRef ...).PeriodPassed("period value will go here") called 1 time; (signature: ...)
The period value will be shown in the Invocations list. 期间值将显示在“调用”列表中。
Another way to do this, is to extract the matcher into a separate lambda and use a breakpoint: Predicate<Period> matcher = e => e.Length == expectedLength; Mock.Assert(() => activityListenerMock.PeriodPassed( Arg.Matches<Period>(e => matcher(e))));
另一种方法是将匹配器提取到一个单独的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))));
Now you can place a breakpoint inside the predicate and check the value of the e
argument. 现在,您可以在谓词中放置断点并检查
e
参数的值。 This works because now the predicate is not an expression but an actual function, so now you can debug it. 这是有效的,因为现在谓词不是表达式而是实际函数,所以现在你可以调试它。
Just to what Stefan Dragnev wrote. 就像Stefan Dragnev写的那样。 I used his idea and then added logic to validate the input.
我使用他的想法,然后添加逻辑来验证输入。 If not the expected value called Assert.Fail().
如果不是预期值,则调用Assert.Fail()。 Not sure if there is a better way, but this works:
不确定是否有更好的方法,但这有效:
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;
}
Adding additional arrange before also worked for me but it is very hacky: 添加额外的安排之前也为我工作,但它是非常hacky:
Mock.Arrange(() => activityListenerMock.PeriodPassed(Arg.IsAny<Period>())).
DoInstead((Period p) => Console.WriteLine("Actual " + p.Length+" expected "+expectedLength));
Ran into this same dilemma today and started expanding on Krzysztof's idea some with an extension. 今天陷入同样的困境,并开始扩展Krzysztof的想法,扩展一些。 It's rough but functional.
它虽然粗糙但功能齐全。
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();
}
}
So far using it this way allows for it be piped along in the normal flow. 到目前为止,以这种方式使用它允许它在正常流程中进行管道传输。
Mock.Arrange(() => foo.bar(Arg.IsAny<Widget>(), Arg.IsAny<WidgetTypeEnum>()))
.PrintParams<Widget, WidgetTypeEnum>()
.MustBeCalled();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.