Skip to content

Commit a8024af

Browse files
committed
Merge pull request #5 from rspec/rails-have-extensions
Extract Rails extension to have matcher
2 parents 5731629 + 679bc26 commit a8024af

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

Gemfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ gemspec
1212
end
1313
end
1414

15-
# only the master branche is supported on rspec-support
15+
# only the master branch is supported on rspec-support
1616
gem "rspec-support", :git => "git://github.com/rspec/rspec-support.git"
1717

1818
gem "cucumber", "~> 1.1.9"
1919
gem "aruba", "~> 0.5"
2020
gem "rake", "~> 10.0.0"
2121

22+
# for optional rails support
23+
gem 'activesupport', RUBY_VERSION < "1.9.3" ? '~> 3.0' : '>= 3.0'
24+
2225
platform :rbx do
2326
gem 'rubysl'
2427
end

lib/rspec/collection_matchers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
require 'rspec/collection_matchers/version'
22
require 'rspec/collection_matchers/matchers'
33
require 'rspec/collection_matchers/have'
4+
5+
begin
6+
require 'rspec/collection_matchers/rails/have_extensions'
7+
rescue LoadError
8+
# must not have rails
9+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'active_support/concern'
2+
require 'active_support/core_ext/module/aliasing'
3+
4+
module RSpec
5+
module CollectionMatchers
6+
module Rails
7+
module HaveExtensions
8+
extend ActiveSupport::Concern
9+
10+
# @api private
11+
#
12+
# Enhances the failure message for `to have(n)` matchers
13+
def failure_message_with_errors_on_extensions
14+
return errors_on_message(:expected, ", got #{@actual}") if is_errors_on?
15+
failure_message_without_errors_on_extensions
16+
end
17+
18+
# @api private
19+
#
20+
# Enhances the description for `to have(n)` matchers
21+
def description_with_errors_on_extensions
22+
return errors_on_message(:have) if is_errors_on?
23+
description_without_errors_on_extensions
24+
end
25+
26+
included do
27+
alias_method_chain :failure_message, :errors_on_extensions
28+
alias_method_chain :description, :errors_on_extensions
29+
30+
## For RSpec 2.99 compatibility
31+
rspec_2x_matcher_failure_method = :failure_message_for_should
32+
rspec_2x_matcher_failure_method = rspec_2x_matcher_failure_method.to_s if RUBY_VERSION < '1.9'
33+
if instance_methods.include? rspec_2x_matcher_failure_method
34+
alias failure_message_for_should_with_errors_on_extensions failure_message_with_errors_on_extensions
35+
alias_method_chain :failure_message_for_should, :errors_on_extensions
36+
alias failure_message_without_errors_on_extensions failure_message_for_should_without_errors_on_extensions
37+
end
38+
end
39+
40+
# @api private
41+
def is_errors_on?
42+
[:errors_on, :error_on].include? @collection_name
43+
end
44+
45+
# @api private
46+
def errors_on_message(prefix, suffix = nil)
47+
"#{prefix} #{relative_expectation} #{@collection_name.to_s.gsub('_', ' ')} :#{@args[0]}#{suffix}"
48+
end
49+
end
50+
end
51+
end
52+
end
53+
54+
RSpec::CollectionMatchers::Have.class_eval do
55+
include RSpec::CollectionMatchers::Rails::HaveExtensions
56+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require "spec_helper"
2+
require 'rspec/collection_matchers/rails/have_extensions'
3+
4+
module RSpec::CollectionMatchers::Rails
5+
describe HaveExtensions do
6+
describe "error_on" do
7+
it "provides a description including the name of what the error is on" do
8+
expect(have(1).error_on(:whatever).description).to eq "have 1 error on :whatever"
9+
end
10+
11+
it "provides a failure message including the number actually given" do
12+
expect {
13+
[].should have(1).error_on(:whatever)
14+
}.to raise_error("expected 1 error on :whatever, got 0")
15+
end
16+
end
17+
18+
describe "errors_on" do
19+
it "provides a description including the name of what the error is on" do
20+
expect(have(2).errors_on(:whatever).description).to eq "have 2 errors on :whatever"
21+
end
22+
23+
it "provides a failure message including the number actually given" do
24+
expect {
25+
[1].should have(3).errors_on(:whatever)
26+
}.to raise_error("expected 3 errors on :whatever, got 1")
27+
end
28+
end
29+
30+
describe "have something other than error_on or errors_on" do
31+
it "has a standard rspec failure message" do
32+
expect {
33+
[1,2,3].should have(2).elements
34+
}.to raise_error("expected 2 elements, got 3")
35+
end
36+
37+
it "has a standard rspec description" do
38+
expect(have(2).elements.description).to eq "have 2 elements"
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)