Deprecate set_the_flash in favor of set_flash

This commit is contained in:
Elliot Winkler 2014-11-21 11:35:38 -07:00
parent 0eb10915db
commit c8fb8022b1
5 changed files with 226 additions and 141 deletions

View File

@ -5,6 +5,9 @@
* `ensure_length_of` has been renamed to `validate_length_of`.
`ensure_length_of` is deprecated and will be removed in 3.0.0.
* `set_the_flash` has been renamed to `set_flash`. `set_the_flash` is
deprecated and will be removed in 3.0.0.
### Bug fixes
* Fix `delegate_method` so that it works again with shoulda-context. ([#591])

View File

@ -1,6 +1,6 @@
require 'shoulda/matchers/action_controller/filter_param_matcher'
require 'shoulda/matchers/action_controller/route_params'
require 'shoulda/matchers/action_controller/set_the_flash_matcher'
require 'shoulda/matchers/action_controller/set_flash_matcher'
require 'shoulda/matchers/action_controller/render_with_layout_matcher'
require 'shoulda/matchers/action_controller/respond_with_matcher'
require 'shoulda/matchers/action_controller/set_session_matcher'

View File

@ -1,7 +1,7 @@
module Shoulda
module Matchers
module ActionController
# The `set_the_flash` matcher is used to make assertions about the
# The `set_flash` matcher is used to make assertions about the
# `flash` hash.
#
# class PostsController < ApplicationController
@ -18,13 +18,13 @@ module Shoulda
# describe 'GET #index' do
# before { get :index }
#
# it { should set_the_flash }
# it { should set_flash }
# end
#
# describe 'DELETE #destroy' do
# before { delete :destroy }
#
# it { should_not set_the_flash }
# it { should_not set_flash }
# end
# end
#
@ -33,13 +33,13 @@ module Shoulda
# context 'GET #index' do
# setup { get :index }
#
# should set_the_flash
# should set_flash
# end
#
# context 'DELETE #destroy' do
# setup { delete :destroy }
#
# should_not set_the_flash
# should_not set_flash
# end
# end
#
@ -60,8 +60,8 @@ module Shoulda
# describe 'GET #index' do
# before { get :index }
#
# it { should set_the_flash[:foo] }
# it { should_not set_the_flash[:bar] }
# it { should set_flash[:foo] }
# it { should_not set_flash[:bar] }
# end
# end
#
@ -70,8 +70,8 @@ module Shoulda
# context 'GET #index' do
# setup { get :show }
#
# should set_the_flash[:foo]
# should_not set_the_flash[:bar]
# should set_flash[:foo]
# should_not set_flash[:bar]
# end
# end
#
@ -91,10 +91,10 @@ module Shoulda
# describe 'GET #index' do
# before { get :index }
#
# it { should set_the_flash.to('A candy bar') }
# it { should set_the_flash.to(/bar/) }
# it { should set_the_flash[:foo].to('bar') }
# it { should_not set_the_flash[:foo].to('something else') }
# it { should set_flash.to('A candy bar') }
# it { should set_flash.to(/bar/) }
# it { should set_flash[:foo].to('bar') }
# it { should_not set_flash[:foo].to('something else') }
# end
# end
#
@ -103,10 +103,10 @@ module Shoulda
# context 'GET #index' do
# setup { get :show }
#
# should set_the_flash.to('A candy bar')
# should set_the_flash.to(/bar/)
# should set_the_flash[:foo].to('bar')
# should_not set_the_flash[:foo].to('something else')
# should set_flash.to('A candy bar')
# should set_flash.to(/bar/)
# should set_flash[:foo].to('bar')
# should_not set_flash[:foo].to('something else')
# end
# end
#
@ -126,9 +126,9 @@ module Shoulda
# describe 'GET #show' do
# before { get :show }
#
# it { should set_the_flash.now }
# it { should set_the_flash[:foo].now }
# it { should set_the_flash[:foo].to('bar').now }
# it { should set_flash.now }
# it { should set_flash[:foo].now }
# it { should set_flash[:foo].to('bar').now }
# end
# end
#
@ -137,20 +137,28 @@ module Shoulda
# context 'GET #index' do
# setup { get :show }
#
# should set_the_flash.now
# should set_the_flash[:foo].now
# should set_the_flash[:foo].to('bar').now
# should set_flash.now
# should set_flash[:foo].now
# should set_flash[:foo].to('bar').now
# end
# end
#
# @return [SetTheFlashMatcher]
# @return [SetFlashMatcher]
#
def set_flash
SetFlashMatcher.new
end
def set_the_flash
SetTheFlashMatcher.new
Shoulda::Matchers.warn_about_deprecated_method(
:set_the_flash,
:set_flash
)
set_flash
end
# @private
class SetTheFlashMatcher
class SetFlashMatcher
def initialize
@options = {}
@value = nil

View File

@ -0,0 +1,60 @@
module UnitTests
module Matchers
def deprecate(old_method, new_method)
DeprecateMatcher.new(old_method, new_method)
end
class DeprecateMatcher
def initialize(old_method, new_method)
@old_method = old_method
@new_method = new_method
end
def matches?(block)
@captured_stderr = capture(:stderr, &block).gsub(/\n+/, ' ')
captured_stderr.include?(expected_message)
end
def failure_message
"Expected block to #{expectation}, but it did not.\nActual warning: #{actual_warning}"
end
alias_method :failure_message_for_should, :failure_message
def failure_message_when_negated
"Expected block not to #{expectation}, but it did."
end
alias_method :failure_message_for_should_not,
:failure_message_when_negated
def description
"should #{expectation}"
end
def supports_block_expectations?
true
end
protected
attr_reader :old_method, :new_method, :captured_stderr
private
def expected_message
"#{old_method} is deprecated and will be removed in the next major release. Please use #{new_method} instead."
end
def expectation
"print a warning deprecating #{old_method} in favor of #{new_method}"
end
def actual_warning
if captured_stderr.empty?
"nothing"
else
"\n #{captured_stderr}"
end
end
end
end
end

View File

@ -1,133 +1,147 @@
require 'unit_spec_helper'
describe Shoulda::Matchers::ActionController::SetTheFlashMatcher do
it 'fails with unmatchable #to' do
expect { set_the_flash.to(1) }.to raise_error('cannot match against 1')
end
context 'a controller that sets a flash message' do
it 'accepts setting any flash message' do
expect(controller_with_flash(notice: 'hi')).to set_the_flash
describe Shoulda::Matchers::ActionController do
describe '#set_the_flash' do
it 'is deprecated in favor of #set_flash' do
expect { set_the_flash }.to deprecate(:set_the_flash, :set_flash)
end
it 'accepts setting the exact flash message' do
expect(controller_with_flash(notice: 'hi')).to set_the_flash.to('hi')
end
it 'accepts setting a matched flash message' do
expect(controller_with_flash(notice: 'hello')).to set_the_flash.to(/he/)
end
it 'rejects setting a different flash message' do
expect(controller_with_flash(notice: 'hi')).
not_to set_the_flash.to('other')
end
it 'rejects setting a different pattern' do
expect(controller_with_flash(notice: 'hi')).
not_to set_the_flash.to(/other/)
it 'still works regardless' do
silence_warnings do
expect(controller_with_flash(notice: 'hi')).to set_the_flash
end
end
end
context 'a controller that sets a flash.now message' do
it 'rejects setting any flash message' do
expect(controller_with_flash_now).not_to set_the_flash
describe '#set_flash' do
it 'fails with unmatchable #to' do
expect { set_flash.to(1) }.to raise_error('cannot match against 1')
end
it 'accepts setting any flash.now message' do
expect(controller_with_flash_now).to set_the_flash.now
end
it 'accepts setting the exact flash.now message' do
expect(controller_with_flash_now(notice: 'hi')).
to set_the_flash.now.to('hi')
end
it 'accepts setting a matched flash.now message' do
expect(controller_with_flash_now(notice: 'flasher')).
to set_the_flash.now.to(/lash/)
end
it 'rejects setting a different flash.now message' do
expect(controller_with_flash_now(notice: 'hi')).
not_to set_the_flash.now.to('other')
end
it 'rejects setting a different flash.now pattern' do
expect(controller_with_flash_now(notice: 'hi')).
not_to set_the_flash.now.to(/other/)
end
end
context 'a controller that sets flash messages for multiple keys' do
it 'accepts flash message for either key' do
controller = controller_with_flash(notice: 'one', alert: 'two')
expect(controller).to set_the_flash[:notice]
expect(controller).to set_the_flash[:alert]
end
it 'rejects a flash message that is not one of the set keys' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_the_flash[:warning]
end
it 'accepts exact flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
to set_the_flash[:notice].to('one')
end
it 'accepts setting a matched flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
to set_the_flash[:notice].to(/on/)
end
it 'rejects setting a different flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_the_flash[:notice].to('other')
end
it 'rejects setting a different pattern' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_the_flash[:notice].to(/other/)
end
end
context 'a controller that sets flash and flash.now' do
it 'accepts setting any flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
context 'a controller that sets a flash message' do
it 'accepts setting any flash message' do
expect(controller_with_flash(notice: 'hi')).to set_flash
end
expect(controller).to set_the_flash.now
expect(controller).to set_the_flash
end
it 'accepts setting a matched flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
it 'accepts setting the exact flash message' do
expect(controller_with_flash(notice: 'hi')).to set_flash.to('hi')
end
expect(controller).to set_the_flash.now.to(/value/)
expect(controller).to set_the_flash.to(/great/)
end
it 'rejects setting a different flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
it 'accepts setting a matched flash message' do
expect(controller_with_flash(notice: 'hello')).to set_flash.to(/he/)
end
expect(controller).not_to set_the_flash.now.to('other')
expect(controller).not_to set_the_flash.to('other')
end
end
it 'rejects setting a different flash message' do
expect(controller_with_flash(notice: 'hi')).
not_to set_flash.to('other')
end
context 'a controller that does not set a flash message' do
it 'rejects setting any flash message' do
expect(controller_with_no_flashes).not_to set_the_flash
it 'rejects setting a different pattern' do
expect(controller_with_flash(notice: 'hi')).
not_to set_flash.to(/other/)
end
end
context 'a controller that sets a flash.now message' do
it 'rejects setting any flash message' do
expect(controller_with_flash_now).not_to set_flash
end
it 'accepts setting any flash.now message' do
expect(controller_with_flash_now).to set_flash.now
end
it 'accepts setting the exact flash.now message' do
expect(controller_with_flash_now(notice: 'hi')).
to set_flash.now.to('hi')
end
it 'accepts setting a matched flash.now message' do
expect(controller_with_flash_now(notice: 'flasher')).
to set_flash.now.to(/lash/)
end
it 'rejects setting a different flash.now message' do
expect(controller_with_flash_now(notice: 'hi')).
not_to set_flash.now.to('other')
end
it 'rejects setting a different flash.now pattern' do
expect(controller_with_flash_now(notice: 'hi')).
not_to set_flash.now.to(/other/)
end
end
context 'a controller that sets flash messages for multiple keys' do
it 'accepts flash message for either key' do
controller = controller_with_flash(notice: 'one', alert: 'two')
expect(controller).to set_flash[:notice]
expect(controller).to set_flash[:alert]
end
it 'rejects a flash message that is not one of the set keys' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_flash[:warning]
end
it 'accepts exact flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
to set_flash[:notice].to('one')
end
it 'accepts setting a matched flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
to set_flash[:notice].to(/on/)
end
it 'rejects setting a different flash message of notice' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_flash[:notice].to('other')
end
it 'rejects setting a different pattern' do
expect(controller_with_flash(notice: 'one', alert: 'two')).
not_to set_flash[:notice].to(/other/)
end
end
context 'a controller that sets flash and flash.now' do
it 'accepts setting any flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
end
expect(controller).to set_flash.now
expect(controller).to set_flash
end
it 'accepts setting a matched flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
end
expect(controller).to set_flash.now.to(/value/)
expect(controller).to set_flash.to(/great/)
end
it 'rejects setting a different flash.now message' do
controller = build_fake_response do
flash.now[:notice] = 'value'
flash[:success] = 'great job'
end
expect(controller).not_to set_flash.now.to('other')
expect(controller).not_to set_flash.to('other')
end
end
context 'a controller that does not set a flash message' do
it 'rejects setting any flash message' do
expect(controller_with_no_flashes).not_to set_flash
end
end
end