Skip to content

Commit

Permalink
Fix wasNotshould throw exception when called on non mocked object
Browse files Browse the repository at this point in the history
Closes #1155
  • Loading branch information
Gosunet committed Feb 19, 2024
1 parent ff79a07 commit 2f3b2e1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
12 changes: 12 additions & 0 deletions modules/mockk-dsl/src/commonMain/kotlin/io/mockk/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,18 @@ class MockKVerificationScope(

@Suppress("UNUSED_PARAMETER")
infix fun List<Any>.wasNot(called: Called) {
if (!all {
MockKDsl.internalIsMockKMock(
mock = it,
regular = true,
spy = true,
objectMock = true,
staticMock = true,
constructorMock = true
)
}) {
throw MockKException("was not can only be called on a mocked object")
}
callRecorder.wasNotCalled(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class StubbingStateTest {
state.recordingDone()
}

verify {
recorder.factories.stubbingAwaitingAnswerState wasNot Called
}
verify(exactly = 0) { recorder.factories.stubbingAwaitingAnswerState(any()) }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ConstructorMockTest {

clearConstructorMockk(MockCls::class)

verify { anyConstructed<MockCls>() wasNot Called }
verify(exactly = 0) { anyConstructed<MockCls>().op(any(), any()) }

assertEquals(3, MockCls().op(1, 2))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.mockk.it

import io.mockk.*
import kotlinx.coroutines.flow.flowOf
import org.junit.jupiter.api.BeforeEach
import kotlin.test.Test
import kotlin.test.assertFailsWith

class WasNotCalledTest {

val mock = mockk<MockCls>()

@Test
fun wasNotCalledOnNonMockedObject() {
assertFailsWith<MockKException>("was not can should throw MockKException on non mock object") {
verify { mock.flowOp(1, 2) wasNot Called }
}
}

@Test
fun wasNotCalledOnMockedObject() {
verify { mock wasNot Called }
}

@Test
fun wasNotCalledShouldThrowAssertionErrorIfMockHasBeenCalled() {
assertFailsWith<AssertionError>("was not can should throw AssertionError if mock has been called") {
every { mock.flowOp(1, 2) } returns flowOf(1, 2)

mock.flowOp(1, 2)
verify { mock wasNot Called }
}
}

class MockCls {
fun flowOp(a: Int = 1, b: Int = 2) = flowOf(a, b)
}
}

0 comments on commit 2f3b2e1

Please sign in to comment.