setting expected "Times" during setup

703 views
Skip to first unread message

mauro.s...@gmail.com

unread,
May 26, 2011, 3:53:31 AM5/26/11
to moq...@googlegroups.com
Hi all,

I'm totally new to Moq, I'm used to Rhino Mock.
I was reading the wiki page on Google Code, imaging this test setup:

var respository = factory.Create<IMyRepository>();
respository.Setup( r => r.MyMethod ( It.IsAny<Int32>() ) ).Verifiable();
...
respository.Verify( r => r. MyMethod( It.IsAny<Int32>() ), Times.Once() );

Don't you think could be interesting to be able to set the expected number of times at setup times and not only at verify time? In this sample there is duplicate code (the expression) required only to set the expected times.

.m
_____________
Mauro Servienti 


Andy McMullan

unread,
May 28, 2011, 1:24:19 PM5/28/11
to moq...@googlegroups.com
Hi Mauro - you don't need the Setup call at all in this case. You
only need the Setup if you want to configure the behaviour of the mock
(return a value, throw an exception etc)

mauro.s...@gmail.com

unread,
May 31, 2011, 3:34:50 AM5/31/11
to moq...@googlegroups.com
Hi Andy,

could you expand on this with a sample?

Cheers,
.m
_____________
Mauro Servienti 

--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com

Andy McMullan

unread,
May 31, 2011, 5:39:04 AM5/31/11
to moq...@googlegroups.com
> could you expand on this with a sample?

// Arrange
var respository = new Mock<IMyRepository>();

// Act
// Do something that uses the (mock) repository

// Assert

mauro.s...@gmail.com

unread,
May 31, 2011, 6:17:56 AM5/31/11
to moq...@googlegroups.com
Ok, understood.
Mine was just a mere sample. If I need to setup "returns" I need to call setup twice, one at setup time and one at verify time. Is it correct?

// Arrange
var respository = new Mock<IMyRepository>();

repository.Setup( r => r. MyMethod( It.IsAny<Int32>() ).Returns( ... );

// Act
// Do something that uses the (mock) repository

// Assert
respository.Verify( r => r. MyMethod( It.IsAny<Int32>() ), Times.Once() );

.m
_____________
Mauro Servienti 


-----Original Message-----
From: moq...@googlegroups.com [mailto:moq...@googlegroups.com] On Behalf Of Andy McMullan
Sent: martedì 31 maggio 2011 11:39
To: moq...@googlegroups.com
Subject: Re: [Moq] setting expected "Times" during setup

--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com

Andy McMullan

unread,
May 31, 2011, 6:44:10 AM5/31/11
to moq...@googlegroups.com
> If I need to setup "returns" I need to call setup twice, one at setup time and one at verify time. Is it correct?

Yes, though in my experience most of the time you don't need to verify
that the call on the mock was made when the mocked method returns a
value, because the assert portion of the test will check that the
returned value was used, and therefore implicitly verify that the
mocked method was called.

Only a small percentage of my tests (< 10% I'd guess) use Verify, and
very few of those Verify calls have a corresponding Setup call.

Daniel Cazzulino

unread,
May 31, 2011, 7:10:44 AM5/31/11
to moq...@googlegroups.com

Ditto here

/kzu from Android

mauro.s...@gmail.com

unread,
May 31, 2011, 7:18:43 AM5/31/11
to moq...@googlegroups.com
Yes it makes sense :-)

Cheers,
.m
_____________
Mauro Servienti 


-----Original Message-----
From: moq...@googlegroups.com [mailto:moq...@googlegroups.com] On Behalf Of Andy McMullan
Sent: martedì 31 maggio 2011 12:44
To: moq...@googlegroups.com
Subject: Re: [Moq] setting expected "Times" during setup

--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages