Calling willReturn multiple times

119 views
Skip to first unread message

Roaders

unread,
Jul 7, 2010, 3:55:24 AM7/7/10
to mockito-flex
Hi All

I am using mockito and for the most part and loving it so far. It is a
lot easier to get your head round that ASMock.

One issue that I am having is that if I set a mock object up but I
want to check that my system under test responds correctly to
properties changing I don't seem to be able to do it.

Look at the following code:

var mockObject : ISomeInterface = mockito.mock( ISomeInterface );

systemUnderTest.delegate = mockObject;

mockito.given( mockObject.someBindableProperty ).willReturn( "string
value 1" );

assertEquals( "string value 1", systemUnderTest.proxiedString ); //
passes

mockito.given( mockObject.someBindableProperty ).willReturn( "string
value 2" );

assertEquals( "string value 2", systemUnderTest.proxiedString // fails

if you make subsequent calls to willReturn for the same property of
method the first value that was set is always returned.

Is there a way round this? Is there a planned fix?

Many Thanks

Kris

unread,
Jul 7, 2010, 4:34:30 AM7/7/10
to mockit...@googlegroups.com
Hi,

I'm glad you like Mockito :).

I think there is a bug filed for it already here.

A workaround for now would be to split the test into two if it's not related to a looping or a multiple calls for the same property.

I'll get to it as soon as I can. There was no big push for it so far.

Thanks for the feedback!

Cheers,
Kris


--
You received this message because you are subscribed to the Google Groups "mockito-flex" group.
To post to this group, send email to mockit...@googlegroups.com.
To unsubscribe from this group, send email to mockito-flex...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito-flex?hl=en.


Kris

unread,
Jul 8, 2010, 3:12:31 AM7/8/10
to mockit...@googlegroups.com
Hi,

Just a thought on working around your problem:

create an Answer implementation that will accept series of the responses you want to return. Then each time the Answer is asked for the response you'd pop one value of the queue.

Usage would be something like:

given(...).will(retunrInSequence("a", "b", "c"));

function returnInSequence(...sequence):Answer
{

Kris

unread,
Jul 8, 2010, 3:17:27 AM7/8/10
to mockit...@googlegroups.com

Sorry my previous email got sent before I finished.



given(...).will(retunrInSequence("a", "b", "c"));

function returnInSequence(...sequence):Answer
{
    return new SequenceAnswer(sequence);
}

class SequenceAnswer implements Answer
{
     private var sequence:Array;

     public function SequenceAnswer(sequence:Array)
     {
         this.sequence = sequence;
     }

   public function give():*
   {
       return sequence.shift();
   }

}
Reply all
Reply to author
Forward
0 new messages