Last active
November 13, 2018 12:56
-
-
Save michaelahlers/5ae2294b7bef6d560197471839021c9e to your computer and use it in GitHub Desktop.
Illustrates why inheritance produces unexpected behavior when an entity may also service as a reference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalamock.scalatest._ | |
import org.scalatest._ | |
import scala.language.implicitConversions | |
class EntityReferenceSpec extends WordSpec with MockFactory { | |
"An entity" that { | |
"is also a reference" when { | |
"subclassing" must { | |
trait MyRef { def id: Long } | |
case class MyEntity(id: Long, name: String) extends MyRef | |
case class MyRefImpl(id: Long) extends MyRef | |
trait MyService { | |
def get(ref: MyRef): MyEntity | |
} | |
"satisfy expectation" in { | |
val service = mock[MyService] | |
(service.get _).expects(MyRefImpl(100)).returns(MyEntity(100, "")) | |
service.get(MyEntity(100, "")) | |
} | |
} | |
"implicitly converted" must { | |
case class MyEntity(id: Long, name: String) | |
case class MyRef(id: Long) | |
object MyRef { | |
implicit def implyMyRef(entity: MyEntity): MyRef = MyRef(entity.id) | |
} | |
trait MyService { | |
def get(ref: MyRef): MyEntity | |
} | |
"satisfy expectation" in { | |
val service = mock[MyService] | |
(service.get _).expects(MyRef(100)).returns(MyEntity(100, "")) | |
service.get(MyEntity(100, "")) | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sbt:entity-reference> testOnly EntityReferenceSpec | |
[info] EntityReferenceSpec: | |
[info] An entity that | |
[info] is also a reference | |
[info] when subclassing | |
[info] - must satisfy expectation *** FAILED *** | |
[info] Unexpected call: <mock-1> MyService.get(MyEntity(100,)) | |
[info] | |
[info] Expected: | |
[info] inAnyOrder { | |
[info] <mock-1> MyService.get(MyRefImpl(100)) once (never called - UNSATISFIED) | |
[info] } | |
[info] | |
[info] Actual: | |
[info] <mock-1> MyService.get(MyEntity(100,)) (Option.scala:121) | |
[info] when implicitly converted | |
[info] - must satisfy expectation | |
[info] ScalaTest | |
[info] Run completed in 143 milliseconds. | |
[info] Total number of tests run: 2 | |
[info] Suites: completed 1, aborted 0 | |
[info] Tests: succeeded 1, failed 1, canceled 0, ignored 0, pending 0 | |
[info] *** 1 TEST FAILED *** | |
[error] Failed: Total 2, Failed 1, Errors 0, Passed 1 | |
[error] Failed tests: | |
[error] EntityReferenceSpec | |
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful | |
[error] Total time: 0 s, completed Aug 3, 2018, 10:43:52 AM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment