Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

RSpec 2.10 compatibility #108

Closed
jcf opened this Issue · 9 comments

7 participants

James Conroy-Finn Mike Burns Jason Draper Gabe Berke-Williams Brian Shaver Valerie Maher Ryan McGeary
James Conroy-Finn

I believe RSpec expects any matcher to respond to both matches? and failure_message_for_should. As shoulda matchers respond to failure_message only I have had to add the following to a support file to get RSpec to recognise shoulda matchers for associations.

require 'shoulda/matchers'

module Shoulda
  module Matchers
    module ActiveRecord
      class AssociationMatcher
        alias_method :failure_message_for_should, :failure_message
      end
    end
  end
end

I think adding an alias to make sure both methods are available should be enough to make shoulda matchers compatible with RSpec 2.10+.

An example error message you will see when running a matcher like…

describe User do
  it { should have_many(:disguises) }
end

…is as follows…

Failure/Error: it { should have_many(:disguises) }
NoMethodError:
  undefined method `has_many?' for #<User:0x007fd2f4f33210>
# ./spec/models/user_spec.rb:2:in `block (2 levels) in <top (required)>'

If you would like me to add aliases and send through a pull request including specs let me know. I'll be happy to help.

EDIT In order to get all custom matchers to work I've hacked the following to my project. It's not going to win any coding style awards, but it works for me.

require 'shoulda/matchers'

module Shoulda
  module Matchers
    [ActiveRecord, ActiveModel, ActionController, ActionMailer].each do |matchers_module|
      matchers_module.constants.each do |constant|
        if constant.to_s =~ /Matcher$/
          class_eval <<-RUBY
            class #{matchers_module}::#{constant}
              alias_method :failure_message_for_should, :failure_message
            end
          RUBY
        end
      end
    end
  end
end
Mike Burns
Admin

#failure_message hasn't been used by RSpec for three years, according to this blame: https://github.com/rspec/rspec-expectations/blame/master/lib/rspec/matchers/compatibility.rb

So let's rename instead of alias. Or does something else depend on the old name?

Jason Draper
Admin

Is there any chance that someone would be utilizing these methods by hand and we should put in deprecations for the old naming?

Gabe Berke-Williams
Admin

I don't imagine so - that's a very manual way to use RSpec, and unless they have their own specs for shoulda-matchers, this shouldn't be a problem.

Gabe Berke-Williams
Admin

See also #101.

This change makes sense to me, and I'd happily look at a pull request.

Brian Shaver

As a side note. This work around doesn't work with guard-spork as far as I can tell, but does running rspec directly.

Edit: This is actually an unrelated cause which I found discussed here:
http://stackoverflow.com/questions/3894232/rspec-shoulda-and-spork-does-not-work-together

Brian Shaver shakerlxxv referenced this issue from a commit in shakerlxxv/shoulda-matchers
Brian Shaver fixes #108 : RSpec 2.10 compatibility 875cd9a
Jason Draper drapergeek closed this issue from a commit
Brian Shaver Rename failure_message and negative_failure_message
* fixes #108 : RSpec 2.10 compatibility
* remove unnecessary attr_readr
4f8684c
Jason Draper drapergeek closed this in 4f8684c
Valerie Maher

I am using shoulda in a new Rails 3.2.13 app, and I'm seeing an error message where the shoulda-context is looking for the failure_message method. I'm wondering if that error is related to this change, and maybe the same rename needs to happen in shoulda-context.

My gem versions are:
shoulda (3.4.0)
shoulda-context (1.0.2)
shoulda-matchers (1.5.4)

I made a very simple Rails app to demonstrate the error:
https://github.com/val99erie/demo_shoulda_error

Steps to reproduce the error:

The error message is:

test: User should require name to be set. (UserTest):
NoMethodError: undefined method failure_message' for #<Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher:0x007fd90d9bcf60>
[...]/gems/shoulda-context-1.0.2/lib/shoulda/context/assertions.rb:60:in
assert_accepts'
[...]/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:332:in block in should'
[...]/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:in
call'
[...]/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:in block in create_test_from_should_hash'
[...]/gems/mocha-0.13.3/lib/mocha/integration/mini_test/version_230_to_2101.rb:36:in
run'

Ryan McGeary
Admin

@val99erie Does the discussion over at thoughtbot/shoulda-context#22 help? I have a proposed course of action for a remedy and would appreciate your feedback.

Ryan McGeary
Admin

@val99erie Just to follow-up with you on this. Please try shoulda-context v1.1.0 that was released today. It supports both the old and new matcher syntax.

Valerie Maher

@rmm5t Yes, it looks like v1.1.0 does fix the problem. I ran the same test with the following gems, and the test is working properly now. Thanks!

gem 'shoulda', '3.4.0'
gem 'shoulda-matchers', '1.5.4'
gem 'shoulda-context', '1.1.0'

panupan panupan referenced this issue from a commit in panupan/shoulda-matchers
Brian Shaver Rename failure_message and negative_failure_message
* fixes #108 : RSpec 2.10 compatibility
* remove unnecessary attr_readr
6d12353
Daniel Duvall marxarelli referenced this issue from a commit in nanowrimo/shoulda-matchers
Brian Shaver Rename failure_message and negative_failure_message
* fixes #108 : RSpec 2.10 compatibility
* remove unnecessary attr_readr
e3ba002
Peter Haza phaza referenced this issue from a commit in phaza/shoulda-matchers
Brian Shaver Rename failure_message and negative_failure_message
* fixes #108 : RSpec 2.10 compatibility
* remove unnecessary attr_readr
123e023
Christopher Chow Soliah referenced this issue
Merged

RSpec 3.0 Support #396

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.