sreedharkesarla / spock

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

Create a example which uses ConfineMetaClassChanges #221

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Describe the enhancement you have in mind.
I have used it something like

<pre><code>
@ConfineMetaClassChanges([<SomeClass>.class])
<SomeOtherClassSpec> extends IntegrationSpec {
...
...
}
</code></pre>

Let me know if it's correct and please do create an example

Original issue reported on code.google.com by raviteja...@gmail.com on 23 Dec 2011 at 11:37

GoogleCodeExporter commented 8 years ago
There is now a simple example in the 1.0-Snapshot docs [1,2].

However, there is a caveat: metaclass changes are only 'confined' for NEW 
instances created within the scope of the annotated method/class.

For example: in a grails integration spec, modifications made to an injected 
service's metaclass will not be reverted after the spec.

@ConfineMetaClassChanges([UserService])
class MySpec extends IngegrationSpec {
  def userService

  void setupSpec() {
    // This does not get cleaned up after the spec !!!
    userService.metaClass.findUserById = { id -> new User('joe') }
  }

  void "foo should do bar"() {
    // ...
  }

}

More Details:
@ConfineMetaClassChanges takes a snapshot of the metaclasses for the specified 
classes before entering the annotated scope, and restores them afterwards, 
using groovy's MetaClassRegistry.

[1] https://github.com/spockframework/spock/pull/30#issuecomment-31571598
[2] 
http://docs.spockframework.org/en/latest/extensions.html#confinemetaclasschanges

Original comment by David.W....@gmail.com on 5 Jan 2014 at 7:47

GoogleCodeExporter commented 8 years ago
Thanks, closing.

Original comment by pnied...@gmail.com on 5 Jan 2014 at 10:44