keeganwitt / gmock

Automatically exported from code.google.com/p/gmock
6 stars 2 forks source link

toString() is not mocked with GString #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Consider:

    void testToStringWithGString() {
        def m1 = mock()
        m1.toString().returns("mock")
        def m2 = mock()
        m2.method("mock").returns(1)
        play {
            assertEquals 1, m2.method("$m1")
        }
    }

If fails with:

junit.framework.AssertionFailedError: Unexpected method call 'method(Mock
for Object (1))' on 'Mock for Object (2)'
  'toString()' on 'Mock for Object (1)': expected 1, actual 0
  'method("mock")' on 'Mock for Object (2)': expected 1, actual 0

It is due to that the internal mode assumes the parameters are evaluated
before the method is called, but GString ("$m1") is lazy evaluated, which
means "m1.toString()" will be invoked in internal mode, so the default
behavior will be invoked.

The solution is simple: when the method is called, iterate its parameters,
if some parameter is GString, evaluate it in external mode.

Original issue reported on code.google.com by JohnnyJianHY on 29 May 2009 at 5:03

GoogleCodeExporter commented 9 years ago

Original comment by julien.g...@gmail.com on 7 Sep 2009 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by JohnnyJianHY on 2 May 2011 at 11:51

GoogleCodeExporter commented 9 years ago

Original comment by JohnnyJianHY on 2 May 2011 at 12:28