Mocking An Object which takes a Closure

24 views
Skip to first unread message

Jozef Dransfield

unread,
Sep 2, 2009, 2:32:31 PM9/2/09
to gmock-user
Anyone help with how i might test something like the method
withTransaction, when i have mocked that class already?

e.g

Code:
DomainClass.withTransaction {
def test = DomainClass.get(1)
test.save()
}


Test:

def expectedDomainClass = mock(DomainClass)
def mockDomainClass = mock(DomainClass)

mockDomainClass.withTransaction(?)
mockDomainClass.static.get(1).returns(expectedDomainClass)
expectedDomainClass.save().returns(true)


How do i match the withTransaction, and then call that code?

Johnny Jian

unread,
Sep 2, 2009, 9:04:36 PM9/2/09
to gmock...@googlegroups.com
We plan to support mocking closures in version 0.9 (please refer to http://code.google.com/p/gmock/issues/detail?id=54), and it is not finished yet.

Julien

unread,
Sep 3, 2009, 4:58:43 AM9/3/09
to gmock-user
There might be a way to mock what you are trying to achieve here.

Create a special mock version of your object like:
class MockDomainClass {
def mock
MockDomainClass(mock){
this.mock = mock
}
def withTransaction(Closure closure){
closure.setDelegate(mock)
closure.call()
}
def methodMissing(String name, args) {
mock."$name"(args.toList())
}
}

I've used this technique in the past. Hopefully it will solve your
problem.

And yes we've plan to support mock for closure but nothing has really
been done so far ;)



On Sep 3, 2:04 am, Johnny Jian <johnnyjia...@gmail.com> wrote:
> We plan to support mocking closures in version 0.9 (please refer tohttp://code.google.com/p/gmock/issues/detail?id=54), and it is not finished
> yet.
>
> On Thu, Sep 3, 2009 at 2:32 AM, Jozef Dransfield
> <jozefdransfi...@gmail.com>wrote:
Reply all
Reply to author
Forward
0 new messages