Skip to content

Commit

Permalink
Merge pull request #1061 from polarene/patch-1
Browse files Browse the repository at this point in the history
Added the "Top Level functions" section
  • Loading branch information
Raibaz committed Mar 6, 2023
2 parents d9bca1c + 81d021c commit 0fa0b26
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 @@ -877,6 +877,24 @@ runTest {

Note: there is a known issue if using a spy with a suspending function: https://github.com/mockk/mockk/issues/554

### Top Level functions

Kotlin lets you declare functions that don’t belong to any class or object, called top-level functions. These calls are translated to static methods in `jvm` environments, and a special Java class is generated to hold the functions. These top-level functions can be mocked using `mockkStatic`. You just need to import the function and pass a reference as the argument:

```kotlin
import com.cars.buildCar

val testCar = Car()
mockkStatic(::buildCar)
every { buildCar() } returns testCar

assertEquals(testCar, buildCar())

verify { buildCar() }
```

Mocking a function doesn't affect other functions declared in the same file.

### Extension functions

There are three types of extension function in Kotlin:
Expand Down

0 comments on commit 0fa0b26

Please sign in to comment.