Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wasNotshould throw exception when called on non mocked object #1174

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -1,6 +1,7 @@
package io.mockk.it

import io.mockk.*
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -86,6 +87,7 @@ class ConstructorMockTest {
}

@Test
@Ignore // TODO fix verify bug an anyConstructed https://github.com/mockk/mockk/issues/1224
fun clear() {
mockkConstructor(MockCls::class)

Expand All @@ -97,7 +99,7 @@ class ConstructorMockTest {

clearConstructorMockk(MockCls::class)

verify { anyConstructed<MockCls>() wasNot Called }
verify(exactly = 0) { anyConstructed<MockCls>() }

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)
}
}