diff --git a/mock4k/README.md b/mock4k/README.md index 3216843..f2862ce 100644 --- a/mock4k/README.md +++ b/mock4k/README.md @@ -12,18 +12,20 @@ Super-simple and super-fast Mocking library. Use when you really don't want to i - ✅ Simple - ✅ Fast - ✅ Relaxed/Strict modes +- ✅ Coroutines +- ✅ Functions - ❌ Verification - ❌ Argument capture -- ❌ Partials +- ❌ Partial mocking - ❌ Spies - ❌ Any evil Powermock nonsense ### Supplementary features: -- ❌ Wannabe-functional -- ❌ Multiplatform - ❌ Spring-support -- ❌ Coroutines +- ❌ Multiplatform - ❌ Annotations +- ❌ Blockchain +- ❌ AI-powered ## Installation @@ -44,7 +46,8 @@ interface Wallet { class AppleStore(private val wallet: Wallet) { fun buyMacBook() = wallet.pay("MacBook", 9999) - fun seeGenius() {} + suspend fun seeGenius() = "hello!" + fun buyMacbookUsing(pay: (String, Int) -> Int?) = pay("MacBook", 9999) } class AppleStoreTest { @@ -67,12 +70,21 @@ class AppleStoreTest { val appleStore = AppleStore(mock(MockMode.Relaxed)) assertThat(appleStore.buyMacBook(), equalTo(null)) } - + @Test fun `see genius`() { - // when there are no calls expected - val appleStore = AppleStore(mock()).seeGenius() - assertThat(appleStore.buyMacBook(), equalTo(Unit)) + val appleStore = AppleStore(mock(Relaxed)) + + runBlocking { + // suspend calls + assertThat(appleStore.seeGenius(), equalTo("hello!")) + } + } + + @Test + fun `bus using someone else's money`() { + val appleStore = AppleStore(mock()) + appleStore.buyMacbookUsing(mock()) } } ``` diff --git a/mock4k/build.gradle b/mock4k/build.gradle index 90f0deb..8e9c163 100644 --- a/mock4k/build.gradle +++ b/mock4k/build.gradle @@ -1 +1,5 @@ description = 'ForkHandles: The very cheapest mocking framework platform.' + +dependencies { + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") +} diff --git a/mock4k/src/test/kotlin/dev/forkhandles/mock4k/MockTest.kt b/mock4k/src/test/kotlin/dev/forkhandles/mock4k/MockTest.kt index 5689c7f..39849d4 100644 --- a/mock4k/src/test/kotlin/dev/forkhandles/mock4k/MockTest.kt +++ b/mock4k/src/test/kotlin/dev/forkhandles/mock4k/MockTest.kt @@ -5,6 +5,7 @@ import com.natpryce.hamkrest.assertion.assertThat import com.natpryce.hamkrest.equalTo import dev.forkhandles.mock4k.MockMode.Relaxed import dev.forkhandles.mock4k.MockMode.Strict +import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test import org.junit.jupiter.api.fail @@ -14,9 +15,12 @@ interface Wallet { class AppleStore(private val wallet: Wallet) { fun buyMacBook() = wallet.pay("MacBook", 9999) - fun seeGenius(): String? = if (true) "sorted" else null + suspend fun seeGenius(): String = "sorted" + fun buyMacbookUsing(fn: (String, Int) -> Int?) = fn("MacBook", 9999) + suspend fun buyMacbookLater() = wallet.pay("MacBook", 9999) } + class MockTest { @Test fun `mock call is strict by default`() { @@ -48,14 +52,45 @@ class MockTest { assertThat(appleStore.buyMacBook(), absent()) } + @Test + fun `suspend mock call supported`() { + val appleStore = AppleStore(mock()) + + runBlocking { + assertThat(appleStore.seeGenius(), equalTo("sorted")) + } + } + + @Test + fun `function mock supported`() { + try { + AppleStore(mock()).buyMacbookUsing(mock()) + fail("didn't throw") + } catch (e: UnstubbedCall) { + assertThat(e.message, equalTo("Unstubbed call: Function2.invoke(MacBook, 9999)")) + } + } + @Test fun `fails on unexpected call`() { try { - val appleStore = AppleStore(mock()) - appleStore.buyMacBook() + AppleStore(mock()).buyMacBook() fail("didn't throw") } catch (e: UnstubbedCall) { assertThat(e.message, equalTo("Unstubbed call: Wallet.pay(MacBook, 9999)")) } } + + @Test + fun `suspend mock call failure`() { + + runBlocking { + try { + AppleStore(mock()).buyMacbookLater() + fail("didn't throw") + } catch (e: UnstubbedCall) { + assertThat(e.message, equalTo("Unstubbed call: Wallet.pay(MacBook, 9999)")) + } + } + } }