Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
kshired committed Nov 5, 2023
1 parent f13cc57 commit aeb33df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1157,15 +1157,15 @@ every { quit(1) } throws Exception("this is a test")

### Scoped mocks

A Scoped mock is a mock that automatically unmocks itself after the code block passed as a parameter has been executed
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
}

// Should be "ObjBeingMocked will be unmocked after this scope"
// ObjBeingMocked will be unmocked after this scope
mockkObject(ObjBeingMocked) {
assertEquals(3, ObjBeingMocked.add(1, 2))
every { ObjBeingMocked.add(1, 2) } returns 55
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class SealedInterfaceTest {
assertEquals(Leaf(1), result)
}

@Test
fun serviceAnswersSealedClassImpl2() {
val factory = mockk<Factory> {
every { create() } answers { Parent(1) }
}

val result = factory.create()

assertEquals(Leaf(1), result)
}

@Test
fun serviceTakesSealedInterfaceAsInput() {
val formattedNode = "Formatted node"
Expand All @@ -49,6 +60,9 @@ class SealedInterfaceTest {
data class Root(val id: Int) : Node
data class Leaf(val id: Int) : Node

@JvmInline
value class Parent(val id: Int) : Node

interface Factory {
fun create(): Node

Expand Down

0 comments on commit aeb33df

Please sign in to comment.