Skip to content
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

Fix now qualifier on set_flash when is called after the key and expected message is set #752

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 5 additions & 1 deletion lib/shoulda/matchers/action_controller/set_flash_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def initialize
def now
store = FlashStore.now
@underlying_matcher = SetSessionOrFlashMatcher.new(store)
@underlying_matcher[key]
@underlying_matcher.to(expected_value)
self
end

Expand All @@ -184,18 +186,20 @@ def in_context(context)
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
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def in_context(context)
end

def [](key)
@key = key
if key
@key = key
end
self
end

Expand All @@ -26,7 +28,9 @@ def to(expected_value = nil, &block)

@expected_value = context.instance_eval(&block)
else
@expected_value = expected_value
if expected_value
@expected_value = expected_value
end
end

self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,48 @@ def store_within(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 'does not match when controller set flash' do
controller = build_fake_response do
flash['key for flash'] = 'value for flash'
end

expect(controller).not_to set_flash['key for flash'].now
end

it 'match when controller set flash.now' do
controller = build_fake_response do
flash.now['key for flash.now'] = 'value for flash.now'
end

expect(controller).to set_flash['key for flash.now'].now
end
end

context 'when the now qualifier is called after the to is called' do
it 'does not match when controller set flash' do
controller = build_fake_response do
flash['to for flash'] = 'value for flash'
end

expect(controller).not_to set_flash['to for flash'].to('value for flash').now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

end

it 'match when controller set flash.now' do
controller = build_fake_response do
flash.now['to for flash.now'] = 'value for flash.now'
end

expect(controller).to set_flash['to for flash.now'].to('value for flash.now').now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [87/80]

end

it 'does not match when controller set flash.now with a different message' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]

controller = build_fake_response do
flash.now['to for flash.now'] = 'value for flash.now'
end

expect(controller).not_to set_flash['to for flash.now'].to('other message').now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]

end
end
end