The example assumes that we're writing code to control a mechanical turtle, similar to that used by Logo programs. Mocking is useful in this kind of situation because we might want to create tests that function even if we don't have the hardware to hand, which run more quickly than would be the case if we ran on real hardware, and where we can use mocks to simulate errors or other situations difficult to reproduce on demand.
"org.scalamock" %% "scalamock-scalatest-support" % "3.0" % "test"
We can now write a test. We'll create a mock turtle that pretends to start at the origin (0, 0) and verifies that if we draw a line from (1, 1) to (2, 1) it performs the correct sequence of turns and movements.
This should (hopefully!) be self-explanatory, with one possible exception. The tilde (~) operator represents an epsilon match, useful for taking account of rounding errors when dealing with floating-point values.
Is there a way to stub objects as with ScalaMock2? Can't seem to find the correct imports.
Can you stub/mock concrete classes with ScalaMock 3?
Hi I have the ditto question – how to mock companion objects or class in this version. Pls guide.
Apologies – there is no way to do this with ScalaMock3 as thing currently stand. There are, sadly, restrictions in Scala's macro system that are currently getting in the way of adding this, and other features. I've not had a chance to look at ScalaMock for several months thanks to working on the "Seven Concurrency Models" book. I hope to be able to look at it again in the new year.
Thanks Paul for the return, I was looking for the same feature.