"optionally verify" ~~ why?

0 views
Skip to first unread message

gerry_lowry (alliston ontario canada (705) 250-0112)

unread,
Nov 20, 2009, 12:12:50 PM11/20/09
to moq...@googlegroups.com
From http://code.google.com/p/moq/:

"Features at a glance
Moq offers the following features:
...
No Record/Reply idioms to learn.
Just construct your mock, set your expectations, use it and
optionally verify them"
^^^^^^^^
^^^^^^^^
^^^^^^^^

Why would one "optionally verify"?

Would one not ALWAYS want to verify?


Regards,
Gerry (Lowry)

Brad Wilson

unread,
Nov 20, 2009, 12:29:49 PM11/20/09
to moq...@googlegroups.com
Not every operation on the Moc is necessarily one that you care to observe. You're content to just let it happen.

gerry_lowry (alliston ontario canada (705) 250-0112)

unread,
Nov 20, 2009, 12:48:45 PM11/20/09
to moq...@googlegroups.com
Brad, I'm really green to Moq in particular and mocking in general.

Would you please be more specific for newbies like myself. A few examples would help me understand your kind reply.

This seems so strange to me. If I choose not to "observe" which I guess means "verify" then how will I know whether my test worked?

Is it not the act of observation/verification that causes a red/green result?

[This reminds me of Erwin Schr�dinger's cat {http://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat}]

How will I know whether my Moq is dead or alive if I do not observe/verify it?

Thank you.
Gerry

Jason Jarrett

unread,
Nov 20, 2009, 2:47:12 PM11/20/09
to moq...@googlegroups.com
One example could be you have an Service like below..

interface IService
{
   void Save(int itemValue);
}

If I have some business logic that ends up calling Save on the IService above,  a particular test may not care that it was called. You may only have to have the interface stubbed out so you can execute your business logic.

Given the following "business logic"

public int BusinessMethod(IService svc)
{
   int a = 1 + 1 + 1;
   svc.Save(a);
   return a;
}

The following test shows that we don't care to "verify" the save method was called, however it did have to create the mock to begin with.

[Test]
public void test()
{
   var mockService = new Mock<IService>()

   var returnedVal = BusinessMethod(mockService.Object);
  Assert.AreEqual(returnedVal, 3);
}

The above test doesn't care that the "Save" method was called with 3, it only cares that it returned 3...

Hope this helps.

Jason

On Fri, Nov 20, 2009 at 9:48 AM, gerry_lowry (alliston ontario canada (705) 250-0112) <gerry...@abilitybusinesscomputerservices.com> wrote:
Brad, I'm really green to Moq in particular and mocking in general.

Would you please be more specific for newbies like myself.  A few examples would help me understand your kind reply.

This seems so strange to me.  If I choose not to "observe" which I guess means "verify" then how will I know whether my test worked?

Is it not the act of observation/verification that causes a red/green result?

[This reminds me of Erwin Schrödinger's cat {http://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat}]

gerry_lowry (alliston ontario canada (705) 250-0112)

unread,
Nov 20, 2009, 3:11:23 PM11/20/09
to moq...@googlegroups.com
Yes, Jason, thank you. Now I get the idea from your excellent example.
Much appreciated.

@ Daniel: a footnote to the quoted feature using Jason's example would be useful to other novices,
especiallly since Moq was "created mainly for developers who aren't currently using
any mocking library", some of whom will be novices like myself.

Gerry.
[This reminds me of Erwin Schr�dinger's cat {http://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat}]

Daniel Cazzulino

unread,
Nov 20, 2009, 3:16:20 PM11/20/09
to moq...@googlegroups.com
thanks for the feedback. added a note to the item.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


[This reminds me of Erwin Schrödinger's cat {http://en.wikipedia.org/wiki/Schr%C3%B6dinger's_cat}]

gerry_lowry (alliston ontario canada (705) 250-0112)

unread,
Nov 20, 2009, 3:44:33 PM11/20/09
to moq...@googlegroups.com
You are welcome. Credit goes to Jason and Brad.

Now, if only Microsoft could respond that quickly!

g.

----- Original Message -----
From: Daniel Cazzulino
To: moq...@googlegroups.com
Sent: Friday, November 20, 2009 3:16 PM
Subject: Re: [Moq] "optionally verify" ~~ why?

thanks for the feedback. added a note to the item.

/kzu

[snip]
Reply all
Reply to author
Forward
0 new messages