Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Share common logic between tests' doc section #779

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion docs/core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,29 @@ You can pass in an optional parameter to `memoized` which controls how the value
- `CachingMode.SCOPE`: effectively a singleton.
- `CachingMode.INHERIT`: internal use only.

If you are using the [gherkin](gherkin.md) style note that the default caching mode is `CachingMode.GROUP`.
If you are using the [gherkin](gherkin.md) style note that the default caching mode is `CachingMode.GROUP`.

### Share common logic between tests
Often there is some code, which needs to be called repeatedly before and after tests.
To have code not duplicated through the code base, Spek allows sharing common logic through the use of Kotlin's extension functions.
Shared setup code can be implemented by declaring an extension function on the `Root` interface.
This function is accessible in every test class or object, which derives from `Spek`.
The call to the `setup()` function is typically placed at the top.
The created objects can then be referenced by name.

```kotlin
// Put the common logic here
fun Root.setup() {
val obj by memoized(
factory = { createObj() }
destructor = { it.dispose() }
)
}

objects Test: Spek({
setup()

// Fetches the object keyed by name 'obj'
val obj: Object by memoized()
})
```