Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

3.0: Rails 4.1 inversible associations #899

Merged
merged 4 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 28, 2013
  1. @alindeman
  2. @alindeman

    :hammer:

    alindeman authored
  3. @alindeman
  4. @alindeman

    Changelog

    alindeman authored
    [ci skip]
This page is out of date. Refresh to see the latest.
View
1  Changelog.md
@@ -4,6 +4,7 @@
Bug fixes
* Use `__send__` rather than `send` to prevent naming collisions (Bradley Schaefer)
+* Supports Rails 4.1. (Andy Lindeman)
Enhancements
View
2  Gemfile
@@ -45,7 +45,7 @@ eval File.read(custom_gemfile) if File.exist?(custom_gemfile)
version_file = File.expand_path("../.rails-version", __FILE__)
case version = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp)
when /master/
- gem "rails", :git => "git://github.com/rails/rails.git"
+ gem "rails", :git => "git://github.com/alindeman/rails.git", :branch => "issue_13390"
gem "arel", :git => "git://github.com/rails/arel.git"
gem "journey", :git => "git://github.com/rails/journey.git"
gem "activerecord-deprecated_finders", :git => "git://github.com/rails/activerecord-deprecated_finders.git"
View
20 lib/rspec/rails/mocks.rb
@@ -29,6 +29,19 @@ def respond_to?(message, include_private=false)
end
end
+ # Starting with Rails 4.1, ActiveRecord associations are inversible
+ # by default. This class represents an association from the mocked
+ # model's perspective.
+ #
+ # @private
+ class Association
+ attr_accessor :target, :inversed
+
+ def initialize(association_name)
+ @association_name = association_name
+ end
+ end
+
module ActiveRecordInstanceMethods
# Stubs `persisted?` to return `false` and `id` to return `nil`.
def destroy
@@ -45,6 +58,13 @@ def [](key)
def new_record?
!persisted?
end
+
+ # Returns an object representing an association from the mocked
+ # model's perspective. For use by Rails internally only.
+ def association(association_name)
+ @associations ||= Hash.new { |h, k| h[k] = Association.new(k) }
+ @associations[association_name]
+ end
end
# Creates a test double representing `string_or_model_class` with common
View
23 spec/rspec/rails/mocks/mock_model_spec.rb
@@ -53,6 +53,29 @@
end
end
+ describe "association" do
+ it "constructs a mock association object" do
+ model = mock_model(MockableModel)
+ expect(model.association(:association_name)).to be
+ end
+
+ it "returns a different association object for each association name" do
+ model = mock_model(MockableModel)
+ posts = model.association(:posts)
+ authors = model.association(:authors)
+
+ expect(posts).not_to equal(authors)
+ end
+
+ it "returns the same association model each time for the same association name" do
+ model = mock_model(MockableModel)
+ posts1 = model.association(:posts)
+ posts2 = model.association(:posts)
+
+ expect(posts1).to equal(posts2)
+ end
+ end
+
describe "errors" do
context "default" do
it "is empty" do
Something went wrong with that request. Please try again.