Skip to content

Commit

Permalink
Split up documentation into two samples
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasTu committed Oct 1, 2023
1 parent 1edba04 commit eec3769
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
12 changes: 10 additions & 2 deletions docs/interaction_based_testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,19 @@ A custom detached mock creator could look like this (please figure out by yourse
include::{sourcedir}/interaction/DetachedMockFactoryDocSpec.groovy[tags=custom-mock-creator]
----

A parametrized feature method showcasing a set of usage scenarios goes as follows:
The first parametrized feature uses the mock without attach:

[source,groovy,indent=0]
----
include::{sourcedir}/interaction/DetachedMockFactoryDocSpec.groovy[tags=use-custom-mock-creator]
include::{sourcedir}/interaction/DetachedMockFactoryDocSpec.groovy[tags=use-custom-mock-creator-no-attach]
----

The next parametrized feature method showcasing a set of usage scenarios when a detached mock
is attached to a spec before usage:

[source,groovy,indent=0]
----
include::{sourcedir}/interaction/DetachedMockFactoryDocSpec.groovy[tags=use-custom-mock-creator-attach]
----

== Further Reading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ class DetachedMockFactoryDocSpec extends Specification {
}
// end::auto-attach[]

// tag::use-custom-mock-creator[]
// tag::use-custom-mock-creator-no-attach[]
@Unroll("Engine state #engineStateResponseType")
def "Manually attach detached mock with preconfigured engine state"() {
def "Mock usage without manually attach detach with preconfigured engine state"() {
given:
def car = new Car(engine: preconfiguredEngine)

// The preconfigured mock with default behaviour behaves as defined,
// even *without* attaching it to the spec.

Expand All @@ -87,16 +86,31 @@ class DetachedMockFactoryDocSpec extends Specification {
then:
possibleResponsesAfterStop.contains(preconfiguredEngine.isStarted())

// Now, let's attach the mock to the spec and override its default behaviour.
where:
engineStateResponseType | possibleResponsesAfterStart | possibleResponsesAfterStop
ALWAYS_STARTED | [true] | [true]
ALWAYS_STOPPED | [false] | [false]
RANDOMLY_STARTED | [true, false] | [true, false]
REAL_RESPONSE | [true] | [false]
preconfiguredEngine = EngineMockCreator.getMock(engineStateResponseType)
}
// end::use-custom-mock-creator-no-attach[]


when:
// tag::use-custom-mock-creator-attach[]
@Unroll("Engine state #engineStateResponseType")
def "Manually attach detached mock with preconfigured engine state"() {
given:
def car = new Car(engine: preconfiguredEngine)
//Now, let's attach the mock to the spec and override its default behaviour.
mockUtil.attachMock(preconfiguredEngine, this)
preconfiguredEngine.isStarted() >> true
then:

expect:
preconfiguredEngine.isStarted()
// The attached now behaves differently. Because it has been attached to the
// The attached mock now behaves differently. Because it has been attached to the
// spec, we can also verify interactions using '1 * ...' or similar, which
// would not be possible before attaching it.
// would not be possible without attaching it.

when:
car.drive()
Expand All @@ -121,7 +135,7 @@ class DetachedMockFactoryDocSpec extends Specification {
REAL_RESPONSE | [true] | [false]
preconfiguredEngine = EngineMockCreator.getMock(engineStateResponseType)
}
// end::use-custom-mock-creator[]
// end::use-custom-mock-creator-attach[]

static
// tag::engine[]
Expand Down

0 comments on commit eec3769

Please sign in to comment.