Skip to content

Add Change Matcher #416

Closed
Closed

Description

@paulz

What did you do?

Write a spec that verifies change in the collection

What did you expect to happen?

To use

expect({code}).toChange(collection.count).by(1)

What actually happened instead?

let count = collection.count
subject()
expect(collection.count - count) == -1

Environment

  • Quick (1.1.0)
  • Nimble (6.0.1)
  • Xcode Version: 8.2.1
  • Swift Version: 3

Please also mention which package manager you used and its version. Delete the
other package managers in this list:

  • Cocoapods: 1.2.0

Project that demonstrates the issue

Here is an example of change matcher from RSpec:
https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/change-matcher

Activity

  1. ikesyo commented on May 14, 2020

    @ikesyo
    Member

    I'm sorry for the really late response but this looks interesting!

  2. paulz commented on May 14, 2020

    @paulz
    Author

    thank you for the reply @ikesyo. This still would be useful for our projects. With the change matcher, we can be assured that tests did not pass accidentally because the value was already matching expected. Also, it will help us writing tests without storing initial values in the variable that we have to name. Currently, we are writing preconditions for tests in "before each" block, which is less readable.

  3. ikesyo commented on May 18, 2020

    @ikesyo
    Member

    A very simple PoC:

    func change(_ subject: @autoclosure @escaping () -> Int, by delta: Int) -> Predicate<Void> {
        return Predicate.define { expr -> PredicateResult in
            let before = subject()
            try expr.evaluate()
            let after = subject()
    
            let diff = after - before
            return PredicateResult(
                bool: diff == delta,
                message: .expectedTo("change the subject by \(delta), but did change by \(diff)")
            )
        }
    }
  4. paulz commented on May 18, 2020

    @paulz
    Author

    Love it!
    and could that be even generic <T: Equatable> or Comparable?

  5. ikesyo commented on May 18, 2020

    @ikesyo
    Member

    For change(_:by:) matcher AdditiveArithmetic would be appropriate to calculate a diff.

    Equatable could be used for change(_:from:to:) matcher.

  6. locked and limited conversation to collaborators on May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions