You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
val failingMock: Foo = mock[Foo].returnsMT[ErrorOr, MyClass](*) returns Left(Error("error"))
749
749
```
750
750
751
+
## Mocking Scala `object`
752
+
753
+
Since version 1.16.0 it is possible to mock `object` methods, given that such definitions are global, the way to mock them is sligtly different in order to ensure we restore the real implementation of the object after we are done
754
+
Example:
755
+
756
+
```scala
757
+
object FooObject {
758
+
def simpleMethod: String = "not mocked!"
759
+
}
760
+
761
+
"mock" should {
762
+
"stub an object method" in {
763
+
FooObject.simpleMethod shouldBe "not mocked!"
764
+
765
+
withObjectMocked[FooObject.type] {
766
+
FooObject.simpleMethod returns "mocked!"
767
+
//or
768
+
when(FooObject.simpleMethod) thenReturn "mocked!"
769
+
770
+
FooObject.simpleMethod shouldBe "mocked!"
771
+
}
772
+
773
+
FooObject.simpleMethod shouldBe "not mocked!"
774
+
}
775
+
}
776
+
```
777
+
778
+
As you can see, the effect of the mocking is only available inside the code block passed to `withObjectMocked`, when such block ends the behavior of the object is restored to its original implementation
0 commit comments