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

Closed
Mik-die opened this Issue Aug 2, 2012 · 5 comments

Comments

Projects
None yet
3 participants

Mik-die commented Aug 2, 2012

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
Owner

dchelimsky commented Aug 2, 2012

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

dchelimsky closed this Aug 2, 2012

Owner

dchelimsky commented Aug 2, 2012

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
Owner

myronmarston commented Aug 2, 2012

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 reopened this Aug 3, 2012

Owner

dchelimsky commented Aug 3, 2012

Reopening since it actually belongs here, after all.

Owner

dchelimsky commented Aug 3, 2012

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

dchelimsky closed this in abfd0fc Aug 3, 2012

@myronmarston myronmarston added a commit that referenced this issue Aug 6, 2012

@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