paulbutcher / ScalaMock

Native Scala mocking framework
http://scalamock.org/
MIT License
501 stars 98 forks source link

explicit wrapping in withExpectations needed when working with specs2 Specification #489

Open poohsen opened 10 months ago

poohsen commented 10 months ago

ScalaMock Version (e.g. 3.5.0)

5.2.0 (specs2: 4.20.2)

Scala Version (e.g. 2.12)

2.13

Runtime (JVM or JS)

JVM

Please describe the expected behavior of the issue

The examples on https://scalamock.org/user-guide/sharing-specs2/ make it look like writing expectations inside a specs2 Specification should work without further boiler-plate as long as the test is wrapped in a in new MockContext {}.

Please provide a description of what actually happens

However, I find that the expectation is ignored if it's not wrapped in a withExpectations call. I'm not sure if the problem is the documentation - the withExpectations boiler-plate needs to be there, or the code itself - withExpectations should be called under the hood somehow but isn't.

Reproducible Test Case

import org.scalamock.specs2.MockContext
import org.specs2.mutable.Specification

class MockTest extends Specification {

  class Testable() {
    def ret(): Int = 2
  }

  "expectation" should {
    "fail" in new MockContext {
      val m = mock[Testable]

      // this should fail IMO but it doesn't.
      // if I wrap it in `withExpectations`, however, it fails when 
      // using `once()` and works when using `never()` - as expected
      (m.ret _).expects().returns(1).once()
    }
  }
}