Refactoring 'then:' clause with verifications into helper method

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

Refactoring 'then:' clause with verifications into helper method

Michał Jedynak
In 'then:' clause I want to invoke a method that includes
verification, e.g.:

----
def "someTestMethod"() {
  when:
  testedObject.doSomething()

  then:
  errorMessageIsDisplayed()
}


def void errorMessageIsDisplayed() {
     1 * collaborator.showErrorMessage()
}
-----
however it doesn't work. If the verification is in 'then:' clause, it
works correctly. Is it possible to do that?

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" 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: Refactoring 'then:' clause with verifications into helper method

Peter Niederwieser
Administrator
You have two options: Either you move the method call into the setup: block (i.e. before the when: block). Or you give Spock a hint:

then:
interaction {
  errorMessageIsDisplayed()
}

This is because Spock, like most other mocking frameworks, has to know about expected interactions beforehand.

Cheers,
Peter

On 17.02.2012, at 22:24, Michał Jedynak wrote:

> In 'then:' clause I want to invoke a method that includes
> verification, e.g.:
>
> ----
> def "someTestMethod"() {
>  when:
>  testedObject.doSomething()
>
>  then:
>  errorMessageIsDisplayed()
> }
>
>
> def void errorMessageIsDisplayed() {
>     1 * collaborator.showErrorMessage()
> }
> -----
> however it doesn't work. If the verification is in 'then:' clause, it
> works correctly. Is it possible to do that?
>
> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" 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 - User" 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: Refactoring 'then:' clause with verifications into helper method

Michał Jedynak
Thanks a lot, second option works for me.

On 17 Lut, 22:29, Peter Niederwieser <[hidden email]> wrote:

> You have two options: Either you move the method call into the setup: block (i.e. before the when: block). Or you give Spock a hint:
>
> then:
> interaction {
>   errorMessageIsDisplayed()
>
> }
>
> This is because Spock, like most other mocking frameworks, has to know about expected interactions beforehand.
>
> Cheers,
> Peter
>
> On 17.02.2012, at 22:24, Michał Jedynak wrote:
>
>
>
>
>
>
>
> > In 'then:' clause I want to invoke a method that includes
> > verification, e.g.:
>
> > ----
> > def "someTestMethod"() {
> >  when:
> >  testedObject.doSomething()
>
> >  then:
> >  errorMessageIsDisplayed()
> > }
>
> > def void errorMessageIsDisplayed() {
> >     1 * collaborator.showErrorMessage()
> > }
> > -----
> > however it doesn't work. If the verification is in 'then:' clause, it
> > works correctly. Is it possible to do that?
>
> > --
> > You received this message because you are subscribed to the Google Groups "Spock Framework - User" 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 - User" 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: Refactoring 'then:' clause with verifications into helper method

Glen Edmonds
In reply to this post by Michał Jedynak
Peter's suggestion didn't work for me, because I didn't have an "interaction" (I was just asserting stuff), but this did:

def verifyStuff(stuff) { 
     assert stuff.foo // etc
     // returning "true" fixes the problem
     true 

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Refactoring 'then:' clause with verifications into helper method

Michael Kutz

You can also declare your method as returning void instead of something (by using def).


Glen Edmonds <[hidden email]> schrieb am Do., 24. Sep. 2015 20:07:
Peter's suggestion didn't work for me, because I didn't have an "interaction" (I was just asserting stuff), but this did:

def verifyStuff(stuff) { 
     assert stuff.foo // etc
     // returning "true" fixes the problem
     true 

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Loading...