set_flash.now works only if called before any other qualifier

In the 3.0.0 `set_flash[:foo].now` is no longer a valid syntax, only the
`set_flash.now[:foo]` is valid[1].

Since this can create a false positive when people updating, we will raise a
exception to make the things more explicit.

[1]: https://github.com/thoughtbot/shoulda-matchers/pull/752
This commit is contained in:
Mauro George 2015-07-08 19:43:10 -03:00 committed by Elliot Winkler
parent 15009c16c3
commit 9268b4940f
3 changed files with 62 additions and 5 deletions

View File

@ -86,6 +86,11 @@
* `set_session['key'].to(nil)` will no longer pass when the key in question
has not been set yet. ([535fe05])
* Change `set_flash` so that `set_flash[:foo].now` is no longer valid syntax.
You'll want to use `set_flash.now[:foo]` instead. This was changed in order to
more closely align with how `flash.now` works when used in a controller.
([#755], [#752])
* Change behavior of `validate_uniqueness_of` when the matcher is not
qualified with any scopes, but your validation is. Previously the following
test would pass when it now fails:
@ -115,6 +120,8 @@
[535fe05]: https://github.com/thoughtbot/shoulda-matchers/commit/535fe05be8686fdafd8b22f2ed5c4192bd565d50
[eaaa2d8]: https://github.com/thoughtbot/shoulda-matchers/commit/eaaa2d83e5cd31a3ca0a1aaa65441ea1a4fffa49
[6ac7b81]: https://github.com/thoughtbot/shoulda-matchers/commit/6ac7b8158cfba3b518eb3da3c24345e4473b416f
[#755]: https://github.com/thoughtbot/shoulda-matchers/pull/755
[#752]: https://github.com/thoughtbot/shoulda-matchers/pull/752
### Bug fixes

View File

@ -129,8 +129,8 @@ module Shoulda
# before { get :show }
#
# it { should set_flash.now }
# it { should set_flash[:foo].now }
# it { should set_flash[:foo].to('bar').now }
# it { should set_flash.now[:foo] }
# it { should set_flash.now[:foo].to('bar') }
# end
# end
#
@ -140,8 +140,8 @@ module Shoulda
# setup { get :show }
#
# should set_flash.now
# should set_flash[:foo].now
# should set_flash[:foo].to('bar').now
# should set_flash.now[:foo]
# should set_flash.now[:foo].to('bar')
# end
# end
#
@ -173,6 +173,10 @@ module Shoulda
end
def now
if key || expected_value
raise QualifierOrderError
end
store = FlashStore.now
@underlying_matcher = SetSessionOrFlashMatcher.new(store)
self
@ -184,18 +188,40 @@ module Shoulda
end
def [](key)
@key = key
underlying_matcher[key]
self
end
def to(expected_value = nil, &block)
@expected_value = expected_value
underlying_matcher.to(expected_value, &block)
self
end
protected
attr_reader :underlying_matcher
attr_reader :underlying_matcher, :key, :expected_value
# @private
class QualifierOrderError < StandardError
def message
<<-MESSAGE.strip
Using `set_flash` with the `now` qualifier and specifying `now` after other
qualifiers is no longer allowed.
You'll want to use `now` immediately after `set_flash`. For instance:
# Valid
should set_flash.now[:foo]
should set_flash.now[:foo].to('bar')
# Invalid
should set_flash[:foo].now
should set_flash[:foo].to('bar').now
MESSAGE
end
end
end
end
end

View File

@ -40,4 +40,28 @@ describe Shoulda::Matchers::ActionController::SetFlashMatcher, type: :controller
expect(controller).not_to set_flash.now['key for flash']
end
end
context 'when the now qualifier is called after the key is set' do
it 'raises a QualifierOrderError' do
controller = build_fake_response
usage = lambda do
expect(controller).to set_flash['any key'].now
end
expect(&usage).to raise_error(described_class::QualifierOrderError)
end
end
context 'when the now qualifier is called after the to qualifier' do
it 'raises a QualifierOrderError' do
controller = build_fake_response
usage = lambda do
expect(controller).to set_flash.to('any value').now
end
expect(&usage).to raise_error(described_class::QualifierOrderError)
end
end
end