jtanistra / spock

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

Test Mixins #218

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
There are situations where a bunch of tests should be applied to a code base. 
We are currently using derived classes which is a pain, especially when your 
code under tests should support either only a subset or  multiple category of 
tests (which results in many abstract base specs and many concrete specs for 
the code under test ...)

This could be solved with test mixins in an elegant way. Here is an example 

class SpecMixin (extends SpecificationMixin?) {

  def "test something"() {
    when:
    objectUnderTest.doSomething()
    then:
    objectUnderTest.somethingDone()
  }

}

@Mixin(SpecMixin)
class RealSpec extends Specification {
  def objectUnderTest = new ObjectUnderTest()
}

The mixin, as well as the test should be able to access properties (as done 
with objectUnderTest in this example) and the test lifecycle methods should be 
mixed in as well.

Original issue reported on code.google.com by peter.ri...@gmx.net on 2 Dec 2011 at 6:16

GoogleCodeExporter commented 8 years ago
I agree that this will be a killer feature. We've wanted to have it for a long 
time. Any volunteers to implement it? :-)

Original comment by pnied...@gmail.com on 13 Dec 2011 at 7:38

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 17 Feb 2012 at 3:04

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 4 Oct 2012 at 6:46

GoogleCodeExporter commented 8 years ago
Try grails.test.mixin.TestMixin

Code will be:
@TestMixin(SpecMixin)
class RealSpec extends Specification {
  def objectUnderTest = new ObjectUnderTest()
}

Original comment by karsten....@gmail.com on 7 Nov 2013 at 4:21

GoogleCodeExporter commented 8 years ago
This issue is about a general solution for Spock, not a Grails-specific one.

Original comment by pnied...@gmail.com on 7 Nov 2013 at 4:27

GoogleCodeExporter commented 8 years ago
I have tried to provide a setup/setupSpec method through a Groovy Mixin. While 
I can provide certain behaviour through inheritance, it turns out to be a bit 
limited when various categories of tests (integration, unit test, ...) require 
a very different test setup. Essentially the class tree will "explode".

Generally I have experienced that Mixins/Delegates are not very helpful with 
Spock specifications. For example I also tried to add a Mixin that provided a 
specification with a Spring @Autowired target that was not recognized by 
Spring. I assume that is due to the way Mixins are implemented in Groovy?

Original comment by Martin.A...@gmail.com on 18 Feb 2014 at 8:45

GoogleCodeExporter commented 8 years ago
Groovy mixins are only visible to Groovy code, and they never appeared to work 
well for me. If I'm not mistaken, there is a way to bring annotations across 
when using `@Delegate`. Then again, `@Delegate` might not play well with other 
AST transforms, such as Spock's.

Original comment by pnied...@gmail.com on 20 Feb 2014 at 1:10

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 1 Mar 2015 at 11:45