简体   繁体   中英

MOQ Unit Test - Assert Vs Verify

Am trying to understand what Exactly Verify or VerifyAll Does ?

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 ?

What extra work does verify does ? Is it the replacement for 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). Verify is used to check if the collaborators of the SUT were notified or contacted.

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.

Verify vs 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

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. 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

If you have a few notifications that you want to be checked, then Verify is easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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