简体   繁体   English

MOQ单元测试-声明与验证

[英]MOQ Unit Test - Assert Vs Verify

Am trying to understand what Exactly Verify or VerifyAll Does ? 我是否在试图确切地了解Verify或VerifyAll的作用?

I was searching and i got the below info on using MOQ 我正在搜索,并且得到了以下有关使用最小起订量的信息

Arrange

Mock
Set up expectations for dependencies
Set up expected results
Create class under test
Act

Invoke method under test
Assert

Assert actual results match expected results
Verify that expectations were met

So What exactly does Verify Does? 那么验证到底能做什么? I can test everything using Assert and in case if any failures the unit test will fail ? 我可以使用Assert测试所有内容,以防万一单元测试失败了?

What extra work does verify does ? 验证还有哪些额外的工作? Is it the replacement for Assert ? 是Assert的替代品吗?

Some more clarify will make me understand. 一些进一步的澄清会使我理解。

Thanks 谢谢

Assert vs Mock.Verify 声明与模拟

Assert is used to do checks on the class/object/subject under test (SUT). 声明用于对类/对象/被测对象(SUT)进行检查。 Verify is used to check if the collaborators of the SUT were notified or contacted. 验证用于检查是否已通知或联系了SUT的协作者。

So if you are testing a car object, which has an engine as a collaborator/dependency. 因此,如果您要测试具有引擎作为协作者/依赖项的汽车对象。 You would use to Assert to see if car.IsHumming after you invoke car.PushStart() You would use Verify to see if _mockEngine.Ignition() received a call. 在调用car.PushStart()之后,您将使用Assert来查看car.IsHumming是否可用。使用Verify来查看_mockEngine.Ignition()是否收到了调用。

Verify vs VerifyAll 验证与verifyAll

Approach One: 方法一:

  1. Explicitly setup all operations you expect to be triggered on the mocked collaborator from the subsequent Act step 明确设置您希望在后续“操作”步骤中在模拟的协作者上触发的所有操作
  2. Act - do something that will cause the operations to be triggered 行动-做一些会导致操作被触发的事情
  3. call _mock.VerifyAll() : to cause every expection that you setup in (1) to be verified 调用_mock.VerifyAll():验证您在(1)中设置的所有期望

Approach Two 方法二

  1. Act - do something that will cause the operations to be triggered 行动-做一些会导致操作被触发的事情
  2. call _mock.Verify(m => m.Operation) : cause verification that Operation was in fact called. 调用_mock.Verify(m => m.Operation):验证实际上已调用Operation。 One Verify call per verification. 每次验证呼叫一个验证电话。 It also allows you to check count of calls eg exactly Once, etc. 它还允许您检查呼叫计数,例如准确地检查一次等。

So if you have multiple operations on the Mock OR if you need the mocked method to return a value which will be processed, then Setup + Act + VerifyAll is the way to go 因此,如果您在Mock上执行了多项操作,或者需要模拟方法返回将要处理的值,则可以使用Setup + Act + verifyAll

If you have a few notifications that you want to be checked, then Verify is easier. 如果您有一些要检查的通知,则“验证”会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM