Skip to content

Commit

Permalink
Added semantic error message when method is same, receiver is same cl…
Browse files Browse the repository at this point in the history
…ass but different object.
  • Loading branch information
SalomonBrys committed Jul 12, 2022
1 parent c1353a0 commit 54bee37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ val kspVersion by extra { "1.7.0-1.0.6" }

allprojects {
group = "org.kodein.mock"
version = "1.8.0"
version = "1.8.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ public class Mocker {
val call = if (mode.exhaustive && mode.inOrder) {
val call = calls.removeFirstOrNull()
?: throw MockerVerificationLazyAssertionError { "Expected a call to ${methodName(receiver, method)} but call list was empty" }
if (method != call.method || receiver !== call.receiver)
if (method != call.method)
throw MockerVerificationLazyAssertionError { "Expected a call to ${methodName(receiver, method)}, but was a call to ${methodName(call.receiver, call.method)}" }
if (receiver !== call.receiver) {
if (receiver != null && call.receiver != null && receiver::class == call.receiver::class) {
throw MockerVerificationLazyAssertionError { "Got a call to ${methodName(receiver, method)}, but expected a different ${receiver::class.simpleName} receiver" }
}
throw MockerVerificationLazyAssertionError { "Expected a call to ${methodName(receiver, method)}, but was a call to ${methodName(call.receiver, call.method)}" }
}
if (constraints.size != call.arguments.size)
throw MockerVerificationLazyAssertionError { "Expected ${constraints.size} arguments to ${methodName(receiver, method)} but got ${call.arguments.size}" }
@Suppress("UNCHECKED_CAST")
Expand Down
5 changes: 4 additions & 1 deletion tests/src/commonTest/kotlin/tests/VerificationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ class VerificationTests {
foo1.newString()
foo2.newString()

var firstHalfPassed = false
val ex = assertFailsWith<AssertionError> {
mocker.verify(inOrder = true) {
foo1.doInt(42)
foo2.doInt(42)
firstHalfPassed = true
// Same method but different receiver order
foo2.newString()
foo1.newString()
}
}
assertEquals("Expected a call to MockFoo.newString(), but was a call to MockFoo.newString()", ex.message)
assertTrue(firstHalfPassed)
assertEquals("Got a call to MockFoo.newString(), but expected a different MockFoo receiver", ex.message)
}

@Test
Expand Down

0 comments on commit 54bee37

Please sign in to comment.