Isolated tests cases are clean and simple, recommended when all test cases have the same
or very similar fixtures
class IsolatedCoffeeMachineTest extends Specification with IsolatedMockFactory {
// shared objectsval 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 must_== true// ...
coffeeMachine.powerOff()
coffeeMachine.isOn must_== false
}
"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 must_== true// ...
}
}
}
A trait that can be mixed into a Specs2 specification to provide mocking support.
To use ScalaMock in Specs2 tests you can either:
Specification
with org.scalamock.specs2.IsolatedMockFactory to get isolated test cases orIsolated tests cases are clean and simple, recommended when all test cases have the same or very similar fixtures