Permalink
Browse files

Merge pull request #428 from alindeman/ar_relation_match_array

[Fixes #398] ActiveRecord::Relation can use =~ matcher
  • Loading branch information...
2 parents 58e89df + 05a2b6c commit 8dfd952517af7ec4221ab11e821bd70235b328bb @justinko justinko committed Aug 19, 2011
@@ -0,0 +1,20 @@
+Feature: ActiveRecord::Relation match array
+
+ The `=~` matcher can be used with an `ActiveRecord::Relation` (scope). The
+ assertion will pass if the scope would return all of the elements specified
+ in the array on the right hand side.
+
+ Scenario: example spec with relation =~ matcher
+ Given a file named "spec/models/widget_spec.rb" with:
+ """
+ require "spec_helper"
+
+ describe Widget do
+ let!(:widgets) { Array.new(3) { Widget.create } }
+ subject { Widget.scoped }
+
+ it { should =~ widgets }
+ end
+ """
+ When I run `rspec spec/models/widget_spec.rb`
+ Then the examples should all pass
@@ -23,3 +23,4 @@ class AssertionFailedError < StandardError
require 'rspec/rails/matchers/be_new_record'
require 'rspec/rails/matchers/be_a_new'
require 'rspec/rails/matchers/have_extension'
+require 'rspec/rails/matchers/relation_match_array'
@@ -0,0 +1,3 @@
+if defined?(:ActiveRecord)
+ RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::MatchArray)
+end
@@ -0,0 +1,19 @@
+require "spec_helper"
+
+describe "ActiveSupport::Relation =~ matcher" do
+ let!(:models) { Array.new(3) { MockableModel.create } }
+
+ it "verifies that the scope returns the records on the right hand side, regardless of order" do
+ MockableModel.scoped.should =~ models.reverse
+ end
+
+ it "fails if the scope encompasses more records than on the right hand side" do
+ another_model = MockableModel.create
+
+ MockableModel.scoped.should_not =~ models.reverse
+ end
+
+ it "fails if the scope encompasses fewer records than on the right hand side" do
+ MockableModel.limit(models.length - 1).should_not =~ models.reverse
+ end
+end
@@ -1,5 +1,4 @@
require 'spec_helper'
-require File.dirname(__FILE__) + '/ar_classes'
describe "mock_model(RealModel)" do
@@ -1,5 +1,4 @@
require 'spec_helper'
-require File.dirname(__FILE__) + '/ar_classes'
describe "stub_model" do
File renamed without changes.

0 comments on commit 8dfd952

Please sign in to comment.