Skip to content

Commit

Permalink
feature[test]: refactoring issue mockk#352
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Scarampella committed Jun 4, 2021
1 parent fefb0c2 commit 6a03bb3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 96 deletions.
86 changes: 0 additions & 86 deletions mockk/common/src/test/kotlin/io/mockk/gh/Issue352Test.kt

This file was deleted.

104 changes: 94 additions & 10 deletions mockk/common/src/test/kotlin/io/mockk/it/CapturingTest.kt
@@ -1,21 +1,19 @@
package io.mockk.it

import io.mockk.*
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class CapturingTest {

class Cls {
var value = 0
}

class MockCls {
fun op(a: Int, b: Int, c: Cls) = a + b + c.value
}
private val mock = mockk<MockedSubject>()

class CoMockCls {
suspend fun op(a: Int, b: Int, c: Cls) = a + b + c.value
@BeforeTest
fun setup() {
every { mock.doSomething("1", "data1") } returns "result1"
every { mock.doSomething("2", "data2") } returns "result2"
}

@Test
Expand Down Expand Up @@ -73,4 +71,90 @@ class CapturingTest {

coVerify { mock.op(1, 2, any()) }
}
}

/**
* See issue #352.
*/
@Test
fun itThrowsAMockkExceptionWhenVerifyingTheSameFunctionTwiceWithSlots() {
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))
}
}
}

/**
* See issue #352.
*/
@Test
fun itDoesNotThrowAMockkExceptionWhenThereAreMultipleTestsVerifyingWithSlots() {
mock.doSomething("1", "data1")

val slot = slot<String>()
verify {
mock.doSomething("1", capture(slot))
}

assertEquals("data1", slot.captured)
}

/**
* See issue #352.
*/
@Test
fun anotherTestToTestTheCoexistenceOfTestsWithSlots() {
mock.doSomething("1", "data1")

val slot = slot<String>()
verify {
mock.doSomething("1", capture(slot))
}

assertEquals("data1", slot.captured)
}

/**
* See issue #352.
*/
@Test
fun itAllowsMultipleCapturingsOfTheSameFunctionUsingAMutableList() {
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])
}

class Cls {
var value = 0
}

class MockCls {
fun op(a: Int, b: Int, c: Cls) = a + b + c.value
}

class CoMockCls {
suspend fun op(a: Int, b: Int, c: Cls) = a + b + c.value
}

open class MockedSubject {
open fun doSomething(id: String?, data: Any?): String {
throw IllegalStateException("Not mocked :(")
}
}
}

0 comments on commit 6a03bb3

Please sign in to comment.