Skip to content

Extract Rails extension to have matcher #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ gemspec
end
end

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

gem "cucumber", "~> 1.1.9"
gem "aruba", "~> 0.5"
gem "rake", "~> 10.0.0"

# for optional rails support
gem 'activesupport', RUBY_VERSION < "1.9.3" ? '~> 3.0' : '>= 3.0'

platform :rbx do
gem 'rubysl'
end
Expand Down
6 changes: 6 additions & 0 deletions lib/rspec/collection_matchers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
require 'rspec/collection_matchers/version'
require 'rspec/collection_matchers/matchers'
require 'rspec/collection_matchers/have'

begin
require 'rspec/collection_matchers/rails/have_extensions'
rescue LoadError
# must not have rails
end
56 changes: 56 additions & 0 deletions lib/rspec/collection_matchers/rails/have_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'active_support/concern'
require 'active_support/core_ext/module/aliasing'

module RSpec
module CollectionMatchers
module Rails
module HaveExtensions
extend ActiveSupport::Concern

# @api private
#
# Enhances the failure message for `to have(n)` matchers
def failure_message_with_errors_on_extensions
return errors_on_message(:expected, ", got #{@actual}") if is_errors_on?
failure_message_without_errors_on_extensions
end

# @api private
#
# Enhances the description for `to have(n)` matchers
def description_with_errors_on_extensions
return errors_on_message(:have) if is_errors_on?
description_without_errors_on_extensions
end

included do
alias_method_chain :failure_message, :errors_on_extensions
alias_method_chain :description, :errors_on_extensions

## For RSpec 2.99 compatibility
rspec_2x_matcher_failure_method = :failure_message_for_should
rspec_2x_matcher_failure_method = rspec_2x_matcher_failure_method.to_s if RUBY_VERSION < '1.9'
if instance_methods.include? rspec_2x_matcher_failure_method
alias failure_message_for_should_with_errors_on_extensions failure_message_with_errors_on_extensions
alias_method_chain :failure_message_for_should, :errors_on_extensions
alias failure_message_without_errors_on_extensions failure_message_for_should_without_errors_on_extensions
end
end

# @api private
def is_errors_on?
[:errors_on, :error_on].include? @collection_name
end

# @api private
def errors_on_message(prefix, suffix = nil)
"#{prefix} #{relative_expectation} #{@collection_name.to_s.gsub('_', ' ')} :#{@args[0]}#{suffix}"
end
end
end
end
end

RSpec::CollectionMatchers::Have.class_eval do
include RSpec::CollectionMatchers::Rails::HaveExtensions
end
42 changes: 42 additions & 0 deletions spec/rspec/collection_matchers/rails/have_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "spec_helper"
require 'rspec/collection_matchers/rails/have_extensions'

module RSpec::CollectionMatchers::Rails
describe HaveExtensions do
describe "error_on" do
it "provides a description including the name of what the error is on" do
expect(have(1).error_on(:whatever).description).to eq "have 1 error on :whatever"
end

it "provides a failure message including the number actually given" do
expect {
[].should have(1).error_on(:whatever)
}.to raise_error("expected 1 error on :whatever, got 0")
end
end

describe "errors_on" do
it "provides a description including the name of what the error is on" do
expect(have(2).errors_on(:whatever).description).to eq "have 2 errors on :whatever"
end

it "provides a failure message including the number actually given" do
expect {
[1].should have(3).errors_on(:whatever)
}.to raise_error("expected 3 errors on :whatever, got 1")
end
end

describe "have something other than error_on or errors_on" do
it "has a standard rspec failure message" do
expect {
[1,2,3].should have(2).elements
}.to raise_error("expected 2 elements, got 3")
end

it "has a standard rspec description" do
expect(have(2).elements.description).to eq "have 2 elements"
end
end
end
end