Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

have_* matchers doesn't work on association extensions #588

Closed
Mik-die opened this Issue · 5 comments

3 participants

@Mik-die

I have association extension, just like in http://guides.rubyonrails.org/association_basics.html#association-extensions

Inside extension I define method has_something?

And then in specs:

# it works for me
customer.orders.has_something?.should be_true 
# It raises NoMethodError: undefined method `has_something?' for []:Array
customer.orders.should have_something
@dchelimsky
Owner

This belongs in rspec-expectations. Would you kindly re-post there?

@dchelimsky dchelimsky closed this
@dchelimsky
Owner

BTW I tried this myself w/ the following code and both examples pass. Please include the code if you repost to rspec-expectations.

require 'spec_helper'

describe Customer do
  example do
    Customer.new.orders.should have_something
  end

  example do
    Customer.new.orders.has_something?.should be_true
  end
end
class Customer < ActiveRecord::Base
  has_many :orders do
    def has_something?
      true
    end
  end
end
@myronmarston
Owner

Here's an explanation for the source of the issue.

The right solution is to use the new expect syntax, since that avoids the problem entirely. However, I wonder if we should add the following to rspec-rails:

RSpec.configure do |config|
  config.expect_with :rspec do |c|
    c.add_should_and_should_not_to ActiveRecord::Associations::CollectionProxy
  end
end

This would make should work for this case (although, I think we should encourage people to use expect to avoid this issue).

Thoughts?

@dchelimsky dchelimsky reopened this
@dchelimsky
Owner

Reopening since it actually belongs here, after all.

@dchelimsky
Owner

We should make the change and encourage ppl to use expect.

@dchelimsky dchelimsky closed this in abfd0fc
@myronmarston myronmarston referenced this issue from a commit
@myronmarston myronmarston A better fix for CollectionProxy#should.
- Delay the fix so that users can configure a different expectation framework.
- Handle the right class for rails 3.0 (AssociationProxy) and 3.1+ (CollectionProxy).

- Fixes #588.
- Fixes #445.
6b4b5cf
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.