Skip to content

Commit 4663873

Browse files
committed
Throw an error when verify block is empty
1 parent d333d68 commit 4663873

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

mokkery-runtime/src/commonMain/kotlin/dev/mokkery/internal/Exceptions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ internal class SingleCallInEveryBlockRequiredException(
5454
}
5555
}
5656
)
57+
internal class SuspiciousEmptyVerifyBlockException : MokkeryRuntimeException(
58+
"Given 'verify' block does not contain any call to a mock. It's very suspicious and most probably caused by misuse.\n\n$noTemplatesCommonReasons"
59+
)
5760

5861
private val noTemplatesCommonReasons = """
5962
Possible reasons:

mokkery-runtime/src/commonMain/kotlin/dev/mokkery/internal/Verify.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ internal fun MokkeryScope.internalVerify(
2626
) {
2727
val scope = createTemplatingScope().apply(block)
2828
val instances = scope.participatingInstances
29+
val templates = scope.registeredTemplates
30+
if (templates.isEmpty()) throw SuspiciousEmptyVerifyBlockException()
2931
instances.withVerifySession {
3032
val result = tools
3133
.verifierFactory
3234
.create(mode, instances)
33-
.verify(this.unverified, scope.registeredTemplates)
35+
.verify(this.unverified, templates)
3436
when (result) {
3537
is Verifier.Result.Success -> result.verified.forEach { this.markVerified(it) }
3638
is Verifier.Result.Failure -> {

test-mokkery/src/commonTest/kotlin/dev/mokkery/test/TemplatingTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ class TemplatingTest {
5656
}
5757
}
5858

59+
@Test
60+
fun testEmptyVerifyCall() {
61+
assertFailsWithEmptyVerifyBlock {
62+
verify { }
63+
}
64+
}
65+
66+
@Test
67+
fun testVerifyWithNonMockCall() {
68+
assertFailsWithEmptyVerifyBlock {
69+
val list = listOf<Int>()
70+
verify { list.size }
71+
}
72+
}
73+
5974
@Test
6075
fun testUnwrapping() {
6176
val mocks = listOf(mock)
@@ -227,6 +242,20 @@ class TemplatingTest {
227242
)
228243
}
229244

245+
private fun assertFailsWithEmptyVerifyBlock(block: () -> Unit) {
246+
assertMokkeryError(
247+
expectedMessage = """
248+
Given 'verify' block does not contain any call to a mock. It's very suspicious and most probably caused by misuse.
249+
250+
Possible reasons:
251+
* You are calling an object that is not a mock.
252+
* You are calling a mock, but the member function is final.
253+
* You are calling a mock, but it's an extension function instead of a member function.
254+
""".trimIndent(),
255+
block = block
256+
)
257+
}
258+
230259
private interface SelfType {
231260

232261
fun callWithSelf(self: SelfType): SelfType

0 commit comments

Comments
 (0)