paulbutcher / ScalaMock

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

MockFactory doesn't catch an expected method call which wasn't actually called if FixtureAny*Spec trait is used #411

Closed arbitrary-dev closed 2 years ago

arbitrary-dev commented 2 years ago

ScalaMock Version

5.1.0 + scalatest-3.2.9

Scala Version

2.13.6

Runtime

OpenJDK 64-Bit Server VM 11.0.10_p9 (build 11.0.10+9, mixed mode, sharing)

Please describe the expected behavior of the issue

[info] TestSpec:
[info] Test
[info] - should fail *** FAILED ***
[info]   Unsatisfied expectation:
[info]   
[info]   Expected:
[info]   inAnyOrder {
[info]     MockFunction0-1() once (never called - UNSATISFIED)
[info]   }
[info]   
[info]   Actual: (TestSpec.scala:10)
[info] Run completed in 498 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***

Please provide a description of what actually happens

[info] TestSpec:
[info] Test
[info] - should fail
[info] Run completed in 329 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.

Reproducible Test Case

import org.scalamock.function.MockFunction0
import org.scalamock.scalatest.MockFactory
import org.scalatest.Outcome
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.FixtureAnyWordSpec

class TestSpec extends FixtureAnyWordSpec with Matchers with MockFactory {

  override type FixtureParam = MockFunction0[Unit]
  override protected def withFixture(test: OneArgTest): Outcome = {
    val mock = mockFunction[Unit]
    test(mock)
  }

  "Test" should {
    "fail" in { fixture =>
      val mock = fixture
      mock.expects().once()
    }
  }
}
barkhorn commented 2 years ago

you override withFixture and by that, disable the verification mechanism that ScalaMock provides. If you want to keep this, you will need to add a withExpectations block yourself for example like this test suite does https://github.com/paulbutcher/ScalaMock/blob/7a1e192de3a65b007315fef0421ca94545562261/shared/src/test/scala/com/paulbutcher/test/mock/MockFunctionTest.scala#L38