Crafting normal interaction behavior template and deviating from it

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Crafting normal interaction behavior template and deviating from it

Erem
Using spock can I override behavior that I specified earlier?
Meaning, if I specified "widget.widge() >> 5", can I immediately
afterwards replace that specification with "widget.widge() >> { throw
new WidgetException() }"?

I ask because I have an object whose central method interacts with a
bunch of different other objects. I want to write different tests that
model when everything's working perfectly but one interaction screws
up. Optimally it would look something like this class:
================

@RunWith(Sputnik)
class Test extends Specification {
        Widget widget = Mock();
        Doodad doodad = Mock();
        ErrorService errService = Mock();

        def testObj = new MyService(widget, doodad, errService)

        //the interactions that happen when everything plays nice
        def everythingGroovy() {
                widget.widge() >> 1
                doodad.doo() >> "dooProduct"
        }

        def "everything working great"() {
                given:
                        everythingGroovy();
                expect: "we get the right response"
                        testObj.serve() == "1dooProduct"
        }

        def "widget is broken"() {
                given:
                        everythingGroovy();
                        //but one thing isn't groovy...
                        widget.widge() >> {throw new WidgetException()}
                when:
                        testObj.serve()
                then: "report an error and throw exception"
                        1 * errService.reportError(_ as WidgetException)
                        thrown(ServiceException)
        }
}

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crafting normal interaction behavior template and deviating from it

Peter Niederwieser
Administrator
It's possible to effectively replace optional (stub) interactions, but be careful. In specs, clarity is usually more important than avoiding code duplication.
That said, either put the overriding interaction in the same scope but before the overridden one (1), or put it in a more narrow scope (i.e. a then block) (2):

1a. (all interactions defined in global scope, overriding first)
given:
widget.widge() >> {throw new WidgetException()}
everythingGroovy()

expect:
...

1b. (all interactions defined in local scope, overriding first)
when:
...

then:
interaction {
  widget.widge() >> {throw new WidgetException()}
  everythingGroovy() // "interaction { ... }" tells Spock that this method defines interactions
}
...

2. (overriding interaction defined in more narrow scope)
given:
everythingGroovy()

when:
...
then:
widget.widge() >> {throw new WidgetException()}
...



Hope this helps.

Cheers,
Peter



On 27.11.2009, at 23:26, Erem wrote:

> Using spock can I override behavior that I specified earlier?
> Meaning, if I specified "widget.widge() >> 5", can I immediately
> afterwards replace that specification with "widget.widge() >> { throw
> new WidgetException() }"?
>
> I ask because I have an object whose central method interacts with a
> bunch of different other objects. I want to write different tests that
> model when everything's working perfectly but one interaction screws
> up. Optimally it would look something like this class:
> ================
>
> @RunWith(Sputnik)
> class Test extends Specification {
> Widget widget = Mock();
> Doodad doodad = Mock();
> ErrorService errService = Mock();
>
> def testObj = new MyService(widget, doodad, errService)
>
> //the interactions that happen when everything plays nice
> def everythingGroovy() {
> widget.widge() >> 1
> doodad.doo() >> "dooProduct"
> }
>
> def "everything working great"() {
> given:
> everythingGroovy();
> expect: "we get the right response"
> testObj.serve() == "1dooProduct"
> }
>
> def "widget is broken"() {
> given:
> everythingGroovy();
> //but one thing isn't groovy...
> widget.widge() >> {throw new WidgetException()}
> when:
> testObj.serve()
> then: "report an error and throw exception"
> 1 * errService.reportError(_ as WidgetException)
> thrown(ServiceException)
> }
> }
>
> --
>
> You received this message because you are subscribed to the Google Groups "Spock Framework" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>
>

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crafting normal interaction behavior template and deviating from it

Erem
Hey Peter,

Definitely did help. Thank you.

On Nov 27, 3:43 pm, Peter Niederwieser <[hidden email]> wrote:

> It's possible to effectively replace optional (stub) interactions, but be careful. In specs, clarity is usually more important than avoiding code duplication.
> That said, either put the overriding interaction in the same scope but before the overridden one (1), or put it in a more narrow scope (i.e. a then block) (2):
>
> 1a. (all interactions defined in global scope, overriding first)
> given:
> widget.widge() >> {throw new WidgetException()}
> everythingGroovy()
>
> expect:
> ...
>
> 1b. (all interactions defined in local scope, overriding first)
> when:
> ...
>
> then:
> interaction {
>   widget.widge() >> {throw new WidgetException()}
>   everythingGroovy() // "interaction { ... }" tells Spock that this method defines interactions}
>
> ...
>
> 2. (overriding interaction defined in more narrow scope)
> given:
> everythingGroovy()
>
> when:
> ...
> then:
> widget.widge() >> {throw new WidgetException()}
> ...
>
> Hope this helps.
>
> Cheers,
> Peter
>
> On 27.11.2009, at 23:26, Erem wrote:
>
> > Using spock can I override behavior that I specified earlier?
> > Meaning, if I specified "widget.widge() >> 5", can I immediately
> > afterwards replace that specification with "widget.widge() >> { throw
> > new WidgetException() }"?
>
> > I ask because I have an object whose central method interacts with a
> > bunch of different other objects. I want to write different tests that
> > model when everything's working perfectly but one interaction screws
> > up. Optimally it would look something like this class:
> > ================
>
> > @RunWith(Sputnik)
> > class Test extends Specification {
> >    Widget widget = Mock();
> >    Doodad doodad = Mock();
> >    ErrorService errService = Mock();
>
> >    def testObj = new MyService(widget, doodad, errService)
>
> >    //the interactions that happen when everything plays nice
> >    def everythingGroovy() {
> >            widget.widge() >> 1
> >            doodad.doo() >> "dooProduct"
> >    }
>
> >    def "everything working great"() {
> >            given:
> >                    everythingGroovy();
> >            expect: "we get the right response"
> >                    testObj.serve() == "1dooProduct"
> >    }
>
> >    def "widget is broken"() {
> >            given:
> >                    everythingGroovy();
> >                    //but one thing isn't groovy...
> >                    widget.widge() >> {throw new WidgetException()}
> >            when:
> >                    testObj.serve()
> >            then: "report an error and throw exception"
> >                    1 * errService.reportError(_ as WidgetException)
> >                    thrown(ServiceException)
> >    }
> > }
>
> > --
>
> > You received this message because you are subscribed to the Google Groups "Spock Framework" group.
> > To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to [hidden email].
> > For more options, visit this group athttp://groups.google.com/group/spockframework?hl=en.

--

You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Loading...