ScalaMock icon indicating copy to clipboard operation
ScalaMock copied to clipboard

Specs2 Example Test Not Failing When Assertion Changed

Open katysrae opened this issue 2 years ago • 0 comments

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:

  • when changing an assertion in OrderSpecification (to expect the mock to be called twice rather than once) a test failure wasn't reported (when running sbt "testOnly com.example.OrderSpecification")
  • adding a "Record-then-Verify style" test, I could write a test with a .verify that wouldn't report a failure when a different product name was 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
    }
  }
}

katysrae avatar May 02 '22 10:05 katysrae