[英]Can't verify Moq method call
There is a problem with verifying TextWriter Write method call, with given params. 在给定参数的情况下验证TextWriter Write方法调用存在问题。 I have this verification code:
我有这个验证码:
_htmlHelperMock.TextWritterMock.Verify(x => x.Write(It.Is<IHtmlString>(p => p == MvcHtmlString.Create("</div>"))), Times.Once);
which throws this exception: 抛出此异常:
Expected invocation on the mock once, but was 0 times: x => x.Write(It.Is<IHtmlString>(p => p == MvcHtmlString.Create("</div>")))
No setups configured.
Performed invocations:
TextWriter.Write(<div class="control-group">)
TextWriter.Write(</div>)
It is interesting that in exception I see the real invocation with strings I want to check. 有趣的是,在例外情况下,我看到了要检查的字符串的真实调用。 How should I configure verify method to check params?
我应该如何配置验证方法来检查参数?
When you are verifying with It.Is<IHtmlString>(p => p == MvcHtmlString.Create("</div>"))
(without my understanding of MvcHtmlString
, it's clear already this will fail). 当您使用
It.Is<IHtmlString>(p => p == MvcHtmlString.Create("</div>"))
(没有我对MvcHtmlString
理解,很显然这将失败)。 Whatever you invoked during the test will be a different object returned by Create
. 在测试期间调用的任何内容都将是
Create
返回的另一个对象。 In this Verify
it is comparing two instances with ==
. 在此
Verify
它正在比较==
两个实例。 These will be object reference equality. 这些将是对象引用相等。
You probably want a Func<IHtmlString,bool>
which compares the value, not the instance. 您可能需要一个
Func<IHtmlString,bool>
来比较值而不是实例。 Are you able to compare a p.ToString()
(or ToHtmlString()
) to simply the string "</div>"
? 您是否可以将
p.ToString()
(或ToHtmlString()
)与字符串"</div>"
? The Create
seems like extra work. Create
似乎是多余的工作。
It.Is<T>
takes a function that says "Given a recorded object of type T
, verify something about that object". It.Is<T>
带有一个函数,该函数说“给定类型为T
的已记录对象,请对该对象进行验证”。 So, this expands to (conceptually): 因此,这扩展为(从概念上):
IHtmlString actual = theRecordedParameter;
IHtmlString expected = MvcHtmlString.Create("</div>");
bool pass = actual == expected;
Assert.IsTrue(pass);
By using some intermediate operations, you can operate on two distinct objects actual and expected, and compare some derived values. 通过使用一些中间操作,您可以对两个不同的实际对象和预期对象进行操作,并比较一些派生值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.