Return matching argument

15 views
Skip to first unread message

Marc

unread,
Jul 14, 2010, 11:21:01 AM7/14/10
to mockito-flex
Maybe it's too hot to think clearly, but how can I return an incoming
argument? For example, the instance "position" should be returned:

assertTrue(position ===
given(car.isCloseTo(position)).willReturn(position))

I wrote the hack below but is there a shorter path?
Thanks,
Marc



var pingpong:PingPong = new PingPong()
given(car.isCloseTo(argThat(pingpong))).will(pingpong)


public class PingPong implements Matcher, Answer
{
private var _point:*

public function matches(point:*):Boolean {
_point = point
return point is Point
}

public function describe():String {
return "Returns the same point in the answer as received in the
matcher."
}

public function give():* {
return _point
}

}

Kris

unread,
Jul 14, 2010, 2:07:25 PM7/14/10
to mockit...@googlegroups.com
Hi Marc,
 
So do you simply need to return an argument that has been passed to the function that matched the criteria? If so there is StubbingContextAware interface that you can use to get access to the original arguments. For example take a look at the answer
org.mockito.impl.CallOriginal
 
When implementing StubbingContextAware in Answer you'll be given StubbinContext gives you access to the passed arguments.
Also take a look a the previous post. One of the users posted some code that also deals with original arguments.
 
Hope that helps!
 
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.


Marc Speck

unread,
Jul 14, 2010, 3:34:28 PM7/14/10
to mockit...@googlegroups.com
Thanks for fully answering my question, Kris.

Keep up the the great work on flex mockito,
Marc

Kris

unread,
Jul 14, 2010, 5:02:24 PM7/14/10
to mockit...@googlegroups.com
Hey do you mind posting the final version of your Answer + some use case?

Thanks!

Marc Speck

unread,
Jul 15, 2010, 2:48:55 AM7/15/10
to mockit...@googlegroups.com
Hey do you mind posting the final version of your Answer + some use case?

Sure. I rewrote it to the code below but I didn't like it. There is too much error throwing involved, so I switched back the the older implementation.

You can see it in the wild under e.g. http://code.google.com/p/blocstac/source/browse/libraries/EditImage/src/test/flex/blocstac/editimage/base/SizePropertiesTest.as


Marc




var pingpong:PingPong = new PingPong()
given(car.isCloseTo(any())).will(pingpong)


public class PingPong implements Answer, StubbingContextAware
{
    private var _point:*
   
    public function give():* {
        if (! _point) {
            throw new ArgumentError("No point is set.")
        }
        return _point
    }
   
    public function useContext(context:StubbingContext):void {
        var args:Array = context.args
        if (args && args.length == 1) {
            _point = args[0]
            if (! _point is Point) {
                throw new TypeError("argument is not of type Point.")
            }
        } else {
            throw new ArgumentError("Only 1 argument expected")
        }
    }
   
}


 

Kris

unread,
Jul 15, 2010, 6:52:16 AM7/15/10
to mockit...@googlegroups.com
Do you consider making it more generic?


public class ReturnArg implements Answer, StubbingContextAware
{
    private var index:int;
    private var context:StubbingContext;

    public function ReturnArg(index:int)
    {
    }

    public function give():* {
        return context.args[index];

    }
   
    public function useContext(context:StubbingContext):void {
       this.context = context;
    }

Flex will tell you both if the returned type is wrong and if there is no such arg in the array.

Hope this helps!
Kris

Marc Speck

unread,
Jul 15, 2010, 7:53:58 AM7/15/10
to mockit...@googlegroups.com
On Thu, Jul 15, 2010 at 12:52 PM, Kris <kris.kar...@gmail.com> wrote:
Do you consider making it more generic?

Well, no I didn't because it's the first time I needed it. But i like it more so I'm using now your solution, http://code.google.com/p/blocstac/source/browse/libraries/EditImage/src/test/flex/blocstac/editimage/base/ReturnArg.as
(just added the assignment of "index" in the constructor)



Flex will tell you both if the returned type is wrong and if there is no such arg in the array.

hmm, generally, I'm very hesitant to rely on the Flex compiler... it's not the finest piece of art...

Thanks again,
Marc



 
Reply all
Reply to author
Forward
0 new messages