jeudi 27 juillet 2017

How to share setup method across multiple tests

I have a pretty big setup() method that sets up a lot of conditions that I need to test across two of my specs. Instead of copy pasting the same setup method in two different specs, I would like the same setup() method to be shared between the two specs.

I noticed that Spock offers setupSpec() method. However, this isn't working for me. It is giving me error: Method on class [com.somepackage.Keyword] was used outside of a Grails application.

Below is an example of what I have:

@TestFor(MyService)
@TestMixin(DomainClassUnitTestMixin)
@Mock([Keyword])
class MyServiceSpec extends Specification {
  def setup() {
     new Keyword(name: "test").save()
  }
}

The above works, however, if I change to setupSpec() then it fails with error Method on class [com.somepackage.Keyword] was used outside of a Grails application.

@TestFor(MyService)
@TestMixin(DomainClassUnitTestMixin)
@Mock([Keyword])
class MyServiceSpec extends Specification {
  def setupSpec() {
     new Keyword(name: "test").save()
  }
}

Question

How can I share the same setup method in MyServiceSpec across multiple specs. I run the test with grails test-app -echoOut -unit MyService

Aucun commentaire:

Enregistrer un commentaire