Package

org.scalamock

specs2

Permalink

package specs2

Visibility
  1. Public
  2. All

Type Members

  1. trait IsolatedMockFactory extends AroundEach with MockContextBase

    Permalink

    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.

    To use ScalaMock in Specs2 tests you can either:

    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 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 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
    	        // ...
    	    }
    	}
    }
  2. trait MockContext extends MockContextBase with Around

    Permalink

    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:

    Fixture contexts are more flexible and are recommened for complex test suites where single set of fixtures does not fit all test cases.

    Basic usage

    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]
     	         // ...
     	     }
     	}
    }

    Complex fixture contexts

    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()
    	     }
    	}
    }
  3. trait MockContextBase extends MockFactoryBase

    Permalink

    Base trait for MockContext and IsolatedMockFactory

  4. trait MockFactory extends AroundEach with MockContextBase

    Permalink

    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:

    Annotations
    @deprecated
    Deprecated

    (Since version 3.2) MockFactory is buggy. Please use IsolatedMockFactory or MockContext instead

Ungrouped