A trait that can be mixed into a Specs2 specification to provide mocking support.
Fixture context that should be created per test-case basis
Fixture context that should be created per test-case basis
To use ScalaMock in Specs2 tests you can either:
Specification
with org.scalamock.specs2.IsolatedMockFactory to
get isolated test cases orFixture contexts are more flexible and are recommened for complex test suites where single set of fixtures does not fit all test cases.
For simple test cases it's enough to run test case in new MockContext
scope.
class BasicCoffeeMachineTest extends Specification { "CoffeeMachine" should { "not turn on the heater when the water container is empty" in new MockContext { val waterContainerMock = mock[WaterContainer] (waterContainerMock.isOverfull _).expects().returning(true) // ... } "not turn on the heater when the water container is overfull" in new MockContext { val waterContainerMock = mock[WaterContainer] // ... } } }
When multiple test cases need to work with the same mocks (and more generally - the same fixtures: files, sockets, database connections, etc.) you can use fixture contexts.
class CoffeeMachineTest extends Specification { trait Test extends MockContext { // fixture context // shared objects val waterContainerMock = mock[WaterContainer] val heaterMock = mock[Heater] val coffeeMachine = new CoffeeMachine(waterContainerMock, heaterMock) // test setup coffeeMachine.powerOn() } // 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() // ... } } "CoffeeMachine" should { "not turn on the heater when the water container is empty" in new MockContext { val heaterMock = mock[Heater] val waterContainerMock = mock[WaterContainer] val coffeeMachine = new CoffeeMachine(waterContainerMock, heaterMock) (waterContainerMock.isOverfull _).expects().returning(true) // ... } "not turn on the heater when the water container is overfull" in new OverfullWaterContainerTest { // ... complexLogic() } } }
Base trait for MockContext and IsolatedMockFactory
A trait that can be mixed into a Specs2 specification to provide mocking support.
A trait that can be mixed into a Specs2 specification to provide mocking support.
MockFactory does not work well in with thread pools and futures. Fixture-contexts support is also broken.
To use ScalaMock in Specs2 tests you can either:
Specification
with org.scalamock.specs2.IsolatedMockFactory to
get isolated test cases or(Since version 3.2) MockFactory is buggy. Please use IsolatedMockFactory or MockContext instead
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