hakandilek / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

groovy mocks are not truthy #316

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
See the example below.
Regular mocks evaluate to true but groovy mocks resolve to false.

class GroovyMockSpec extends Specification {
    def mock = Mock(A)
    def groovyMock = GroovyMock(Object)

    def "regular mocks are truthy"() {
        expect:
        mock ?: 'irrelevant' == mock
    }

    def "groovy mocks are falsey"() {
        expect:
        groovyMock ?: 'unexpected' == 'unexpected'
    }

    static interface A {}
}

What version of Spock and Groovy are you using?
0.7-groovy-1.8

Original issue reported on code.google.com by s...@thinkerit.be on 17 Jun 2013 at 12:36

GoogleCodeExporter commented 9 years ago
Same here: groovy: 2.2.2, spock: 0.7-groovy-2.0

I need it to be working in the same way, as I'm mocking collaborators and with 
GroovyMock they evaluate to false, and throw exceptions. For now I'll use 
regular mocks.

Original comment by janusz.s...@gmail.com on 28 Apr 2014 at 5:22

GoogleCodeExporter commented 9 years ago
Example test http://meetspock.appspot.com/script/5700735861784576

Original comment by janusz.s...@gmail.com on 28 Apr 2014 at 5:28

GoogleCodeExporter commented 9 years ago
Why is your test depending on the truthiness of a mock object? What's the use 
case?

Original comment by pnied...@gmail.com on 28 Apr 2014 at 5:31

GoogleCodeExporter commented 9 years ago
This will do the trick:

def groovyMock = GroovyMock(Map) { asBoolean() >> true }

Perhaps this should be the default. Not sure.

Original comment by pnied...@gmail.com on 28 Apr 2014 at 5:33

GoogleCodeExporter commented 9 years ago
void initialize() {
    if( ! config) {
        throw new RuntimeException('configuration has not been injected')
    }
    // ... initialize logic
}

I'm not saying it's good design to have initialize method, but it'll work for 
now. However GroovyMock config object injected via constructor evaluates to 
false. Using Mock, it's OK.

Original comment by janusz.s...@gmail.com on 28 Apr 2014 at 5:35

GoogleCodeExporter commented 9 years ago
I see. Thanks for the explanation.

By the way, GroovyMock() should only be used where Mock() isn't good enough.

Original comment by pnied...@gmail.com on 28 Apr 2014 at 5:37

GoogleCodeExporter commented 9 years ago
>> def groovyMock = GroovyMock(Map) { asBoolean() >> true }

Great - thanks for reply. Works perfectly :-)

Original comment by janusz.s...@gmail.com on 28 Apr 2014 at 5:39

GoogleCodeExporter commented 9 years ago
I see I used the elvis operator so that's probably the specific case I ran into 
at the time. I suspect I was doing some kind of lookup and found a GroovyMock 
instance but instead I got the value on the right side of the operator.
I don't like having to explicitly define this kind of behavior or the explicit 
Object type on the groovy mock or be forced to use null checking instead of the 
elvis operator.

As for preferring GroovyMock instead of Mock. I understand why you may feel 
that way but I do prefer the dynamic aspect of GroovyMock in some cases. Much 
like how Brian Marick uses dynamic mocks in his Robozzle kata. 
(http://vimeo.com/19404746)

Does it not make more sense for Mock and GroovyMock to behave the same way?

Original comment by s...@thinkerit.be on 28 Apr 2014 at 6:28