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 scoped mock documentation #1175

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,24 @@ every { quit(1) } throws Exception("this is a test")
* clear - deletes the internal state of objects associated with a mock, resulting in an empty object
* unmock - re-assigns transformation of classes back to original state prior to mock

### Scoped mocks

A Scoped mock is a mock that automatically unmocks itself after the code block passed as a parameter has been executed
kshired marked this conversation as resolved.
Show resolved Hide resolved
You can use the `mockkObject`, `mockkStatic` and `mockkConstructor` functions.

```kotlin
object ObjBeingMocked {
fun add(a: Int, b: Int) = a + b
}

// Should be "ObjBeingMocked will be unmocked after this scope"
kshired marked this conversation as resolved.
Show resolved Hide resolved
mockkObject(ObjBeingMocked) {
assertEquals(3, ObjBeingMocked.add(1, 2))
every { ObjBeingMocked.add(1, 2) } returns 55
assertEquals(55, ObjBeingMocked.add(1, 2))
}
```

## Matcher extensibility

A very simple way to create new matchers is by attaching a function
Expand Down