Skip to content

Commit

Permalink
Merge pull request #1175 from kshired/docs/add-scoped-mocks
Browse files Browse the repository at this point in the history
Add scoped mock documentation
  • Loading branch information
Raibaz committed Nov 6, 2023
2 parents a30c745 + d6c5e5f commit 6f2ad62
Showing 1 changed file with 18 additions and 0 deletions.
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.
You can use the `mockkObject`, `mockkStatic` and `mockkConstructor` functions.

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

// ObjBeingMocked will be unmocked after this scope
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

0 comments on commit 6f2ad62

Please sign in to comment.