Permalink
Browse files

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.
  • Loading branch information...
myronmarston committed Aug 6, 2012
1 parent f9ebca7 commit 6b4b5cfe652e1b00e540a1a386938833d31eca9f
View
@@ -1,3 +1,11 @@
+### dev
+[full changelog](http://github.com/rspec/rspec-rails/compare/v2.11.0...master)
+
+Bug fixes
+
+* Add `should` and `should_not` to `CollectionProxy` (Rails 3.1+) and
+ `AssociationProxy` (Rails 3.0). (Myron Marston)
+
### 2.11.0 / 2012-07-07
[full changelog](http://github.com/rspec/rspec-rails/compare/v2.10.1...v2.11.0)
@@ -1 +1,2 @@
require 'rspec/rails/extensions/active_record/base'
+require 'rspec/rails/extensions/active_record/proxy'
@@ -0,0 +1,17 @@
+RSpec.configure do |rspec|
+ # Delay this in order to give users a chance to configure `expect_with`...
+ rspec.before(:suite) do
+ if defined?(RSpec::Matchers)
@dchelimsky

dchelimsky Aug 6, 2012

Owner

We should probably test for whether should is enabled as well.

@jonhyman

jonhyman Aug 18, 2012

Contributor

I'm using Mongoid and just got this error on HEAD due to this change. Projects that use Mongoid don't use ActiveRecord. You should add a check for defined?(ActiveRecord).

/Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/rspec-rails-fb55955687e2/lib/rspec/rails/extensions/active_record/proxy.rb:8:in `block (3 levels) in <top (required)>': uninitialized constant ActiveRecord (NameError)
@jonhyman

jonhyman Aug 18, 2012

Contributor

Heh, nice. I'll close out #599.

@jonhyman

jonhyman Aug 18, 2012

Contributor

Thanks!

+ # In Rails 3.0, it was AssociationProxy.
+ # In 3.1+, it's CollectionProxy.
+ const_name = [:CollectionProxy, :AssociationProxy].find do |const|
+ ActiveRecord::Associations.const_defined?(const)
+ end
+
+ proxy_class = ActiveRecord::Associations.const_get(const_name)
+
+ RSpec::Matchers.configuration.add_should_and_should_not_to proxy_class
+ end
+ end
+end
+
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+class CollectionOwner < ActiveRecord::Base
+ connection.execute <<-SQL
+ CREATE TABLE collection_owners (
+ id integer PRIMARY KEY AUTOINCREMENT
+ )
+SQL
+ has_many :associated_items do
+ def has_some_quality?; true end
+ end
+end
+
+class AssociatedItem < ActiveRecord::Base
+ connection.execute <<-SQL
+ CREATE TABLE associated_items (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ collection_owner_id integer
+ )
+SQL
+ belongs_to :collection_owner
+end
+
+describe "should have_xxx" do
+ #before { RSpec::Rails::Extensions::ActiveRecord::Proxy.fix_should_and_should_not }
@dchelimsky

dchelimsky Aug 6, 2012

Owner

This should be uncommented or removed (I'm assuming removed). Otherwise this all looks good.

+
+ it "works with ActiveRecord::Associations::CollectionProxy" do
+ owner = CollectionOwner.new
+ owner.associated_items.should have_some_quality
+ end
+end

1 comment on commit 6b4b5cf

Owner

myronmarston commented on 6b4b5cf Aug 6, 2012

Good catches -- I just committed a fix for both things.

Please sign in to comment.