Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #533 from mockk/fix-issue-352
Added a check to prevent slots from being used when verifying multiple calls
- Loading branch information
Showing
6 changed files
with
85 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.mockk.gh | ||
|
||
import io.mockk.MockKException | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.slot | ||
import io.mockk.verify | ||
import io.mockk.verifyOrder | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class Issue352Test { | ||
|
||
open class MockedSubject { | ||
open fun doSomething(id: String?, data: Any?): String { | ||
throw IllegalStateException("Not mocked :(") | ||
} | ||
} | ||
|
||
private val mock = mockk<MockedSubject>() | ||
|
||
@BeforeTest | ||
fun setup() { | ||
every { mock.doSomething("1", "data1") } returns "result1" | ||
every { mock.doSomething("2", "data2") } returns "result2" | ||
} | ||
|
||
@Test | ||
fun `It throws a MockkException when verifying the same function twice with slots`() { | ||
mock.doSomething("1", "data1") | ||
mock.doSomething("2", "data2") | ||
|
||
val dataSlotId1 = slot<String>() | ||
val dataSlotId2 = slot<String>() | ||
|
||
assertFailsWith<MockKException> { | ||
verify { | ||
mock.doSomething("1", capture(dataSlotId1)) | ||
mock.doSomething("2", capture(dataSlotId2)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `It allows multiple capturings of the same function using a mutableList`() { | ||
mock.doSomething("1", "data1") | ||
mock.doSomething("2", "data2") | ||
|
||
val slotList = mutableListOf<String>() | ||
|
||
verify { | ||
mock.doSomething("1", capture(slotList)) | ||
mock.doSomething("2", capture(slotList)) | ||
} | ||
|
||
assertEquals("data1", slotList[0]) | ||
assertEquals("data2", slotList[1]) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters