allows combining of macro mocks wih proxy mocks in the same Suite
allows combining of macro mocks wih proxy mocks in the same Suite
val macroMock = mock[Foo] val proxyMock = Proxy.mock[Bar]
Trait that can be mixed into a ScalaTest suite to provide mocking support.
Trait that can be mixed into a ScalaTest suite to provide mocking support.
class CoffeeMachineTest extends FlatSpec with ShouldMatchers with MockFactory { "CoffeeMachine" should "not turn on the heater when the water container is empty" in { val waterContainerMock = mock[WaterContainer] (waterContainerMock.isEmpty _).expects().returning(true) // ... } it should "not turn on the heater when the water container is overfull" in { val waterContainerMock = mock[WaterContainer] // ... } }
Sometimes multiple test cases need to work with the same mocks (and more generally - the same fixtures: files, sockets, database connections, etc.). There are many techniques to avoid duplicating the fixture code across test cases in ScalaTest, but ScalaMock recommends and officially supports these two:
If you mix OneInstancePerTest
trait into a Suite
, each test case will run in its own instance
of the suite class and therefore each test will get a fresh copy of the instance variables.
This way in the suite scope you can declare instance variables (e.g. mocks) that will be used by multiple test cases and perform common test case setup (e.g. set up some mock expectations). Because each test cases has fresh instance variables different test cases do not interfere with each other.
// Please note that this test suite mixes in OneInstancePerTest class CoffeeMachineTest extends FlatSpec with ShouldMatchers with OneInstancePerTest with MockFactory { // shared objects val waterContainerMock = mock[WaterContainer] val heaterMock = mock[Heater] val coffeeMachine = new CoffeeMachine(waterContainerMock, heaterMock) // you can set common expectations in suite scope (waterContainerMock.isOverfull _).expects().returning(true) // test setup coffeeMachine.powerOn() "CoffeeMachine" should "not turn on the heater when the water container is empty" in { coffeeMachine.isOn shouldBe true // ... coffeeMachine.powerOff() } it should "not turn on the heater when the water container is overfull" in { // each test case uses separate, fresh Suite so the coffee machine is turned on coffeeMachine.isOn shouldBe true // ... } }
You can also run each test case in separate fixture context. Fixture contexts can be extended and combined and since each test case uses different instance of fixture context test cases do not interfere with each other while they can have shared mocks and expectations.
class CoffeeMachineTest extends FlatSpec with ShouldMatchers with MockFactory { trait Test { // fixture context // shared objects val waterContainerMock = mock[WaterContainer] val heaterMock = mock[Heater] val coffeeMachine = new CoffeeMachine(waterContainerMock, heaterMock) // test setup coffeeMachine.powerOn() } "CoffeeMachine" should "not turn on the heater when the water container is empty" in new Test { coffeeMachine.isOn shouldBe true (waterContainerMock.isOverfull _).expects().returning(true) // ... } // you can extend and combine fixture-contexts trait OverfullWaterContainerTest extends Test { // you can set expectations and use mocks in fixture-context (waterContainerMock.isEmpty _).expects().returning(true) // and define helper functions def complexLogic() { coffeeMachine.powerOff() // ... } } it should "not turn on the heater when the water container is overfull" in new OverfullWaterContainerTest { // ... complexLogic() } }
See org.scalamock for overview documentation.
Trait that can be mixed into org.scalatest.path.* specs (e.g.
Trait that can be mixed into org.scalatest.path.* specs (e.g. path.FunSpec).
MockFactory cannot be used because it overrides a final method. If you use path.FunSpec you need to verify expectations yourself - just add verifyExpectations at the end of the root-level suite.
See MockFactory for more information.