Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 6b4b5cfe652e1b00e540a1a386938833d31eca9f 1 parent f9ebca7
@myronmarston myronmarston authored
View
8 Changelog.md
@@ -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)
View
1  lib/rspec/rails/extensions.rb
@@ -1 +1,2 @@
require 'rspec/rails/extensions/active_record/base'
+require 'rspec/rails/extensions/active_record/proxy'
View
17 lib/rspec/rails/extensions/active_record/proxy.rb
@@ -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 Owner

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

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)
@dchelimsky Owner

Done: 07d6a1a

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

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # 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
+
View
31 spec/rspec/rails/matchers/has_spec.rb
@@ -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 Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ it "works with ActiveRecord::Associations::CollectionProxy" do
+ owner = CollectionOwner.new
+ owner.associated_items.should have_some_quality
+ end
+end

1 comment on commit 6b4b5cf

@dchelimsky

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

@dchelimsky

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

@myronmarston

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

@jonhyman

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)
Please sign in to comment.
Something went wrong with that request. Please try again.