paulbutcher / ScalaMock

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

Specs2 Example Test Not Failing When Assertion Changed #443

Open katysrae opened 2 years ago

katysrae commented 2 years ago

ScalaMock Version (e.g. 3.5.0)

5.3.0-SNAPSHOT

Scala Version (e.g. 2.12)

2.13.6

Runtime (JVM or JS)

JVM

Please describe the expected behavior of the issue

Specs2 test example should fail when assertion is changed to be not true

Please provide a description of what actually happens

Hello - I'm new to the library and was experimenting with the example test cases provided:

However, changing the test class to mix in IsolatedMockFactory (rather than using MockContext) caused the modified tests to fail (as desired).

Reproducible Test Case

package com.example

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

/**
 * This is a demonstration and test of the Specs2 integration, using the example from
 * Martin Fowler's article Mocks Aren't Stubs http://martinfowler.com/articles/mocksArentStubs.html
 */
class OrderSpecification extends Specification {

  "An order" should {
    "remove inventory when in stock" in new MockContext {
      val mockWarehouse = mock[Warehouse]
      inSequence {
        (mockWarehouse.hasInventory _).expects("Talisker", 50).returning(true).once
        (mockWarehouse.remove _).expects("Talisker", 50).once
      }.twice //no failure reported
      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beTrue
    }

    "remove inventory (Record-then-Verify style)" in new MockContext {
      val mockWarehouse = stub[Warehouse]

      (mockWarehouse.hasInventory _).when("Talisker", 50).returns(true)

      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)

      assert(order.isFilled)
      (mockWarehouse.hasInventory _).verify("DifferentProduct", 50).once //no failure reported
      (mockWarehouse.remove _).verify("DifferentProduct", 50).repeat(2) //no failure reported
    }

    "remove nothing when out of stock" in new MockContext {
      val mockWarehouse = mock[Warehouse]
      (mockWarehouse.hasInventory _).expects(*, *).returning(false).repeat(2) //no failure reported
      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beFalse
    }

    "remove nothing when out of stock (Record-then-Verify style)" in new MockContext {
      val mockWarehouse = stub[Warehouse]
      (mockWarehouse.hasInventory _).when(*, *).returns(false)

      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beFalse
      (mockWarehouse.hasInventory _).verify("DifferentProduct", *).once //no failure reported
    }
  }
}

Failures reported using IsolatedMockFactory


package com.example

import org.scalamock.specs2.IsolatedMockFactory
import org.specs2.mutable.Specification

/**
 * This is a demonstration and test of the Specs2 integration, using the example from
 * Martin Fowler's article Mocks Aren't Stubs http://martinfowler.com/articles/mocksArentStubs.html
 */
class OrderIsolatedSpecification extends Specification with IsolatedMockFactory {

  "An order" should {
    "remove inventory when in stock" in {
      val mockWarehouse = mock[Warehouse]
      inSequence {
        (mockWarehouse.hasInventory _).expects("Talisker", 50).returning(true).once
        (mockWarehouse.remove _).expects("Talisker", 50).once
      }.twice //failure reported
      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beTrue
    }

    "remove inventory (Record-then-Verify style)" in {
      val mockWarehouse = stub[Warehouse]

      (mockWarehouse.hasInventory _).when("Talisker", 50).returns(true)

      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)

      assert(order.isFilled)
      (mockWarehouse.hasInventory _).verify("DifferentProduct", 50).once //failure reported
      (mockWarehouse.remove _).verify("DifferentProduct", 50).repeat(2) //failure reported

      order.isFilled must beTrue //adding here so test compiles
    }

    "remove nothing when out of stock" in {
      val mockWarehouse = mock[Warehouse]
      (mockWarehouse.hasInventory _).expects(*, *).returning(false).repeat(2) //failure reported
      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beFalse
    }

    "remove nothing when out of stock (Record-then-Verify style)" in {
      val mockWarehouse = stub[Warehouse]
      (mockWarehouse.hasInventory _).when(*, *).returns(false)

      val order = new Order("Talisker", 50)
      order.fill(mockWarehouse)
      order.isFilled must beFalse
      (mockWarehouse.hasInventory _).verify("DifferentProduct", *).once //failure reported

      order.isFilled must beFalse //adding here so test compiles
    }
  }
}