Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for "Custom Matcher from scratch" #628

Open
elado opened this issue Aug 15, 2014 · 3 comments
Open

Improve docs for "Custom Matcher from scratch" #628

elado opened this issue Aug 15, 2014 · 3 comments

Comments

@elado
Copy link

elado commented Aug 15, 2014

When creating a custom matcher and using it with a mock argument expectation, the === method should also be defined, otherwise, the match will fail.

Example:

expect(instance).to receive(:method).with(a_matcher(1))

Documentation here - http://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers
Under Custom Matcher from scratch

Add this code to the class:

class BeInZone
  def initialize(expected)
    @expected = expected
  end

  # ...

  alias === matches?

  # ...
end

Alternatively, rspec-mocks should see if this is a matcher and use matches?.

@myronmarston
Copy link
Member

A better approach is to include RSpec::Matchers::Composable; it defines the === alias and provides some other functionality to help use the matcher in composable fashion.

I agree the docs could be improved, though....want to open a PR to improve it?

@cupakromer
Copy link
Member

We may want to link to the matcher protocol that was add in #567.

@elado thanks for reporting this, does lib/rspec/matchers/matcher_protocol.rb provide the details you seek?

@myronmarston
Copy link
Member

We may want to link to the matcher protocol that was add in #567.

That would be good. I also think the examples should be updated to show that we recommend including RSpec::Matchers::Composable as it's intended to be mixed into every matcher class so that it supports the full composability features of matchers in RSpec 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants