Skip to content

Instantly share code, notes, and snippets.

@showaltb
Last active October 20, 2016 02:59
Show Gist options
  • Save showaltb/0456ce0002842c88c3fc06db43f3ee7b to your computer and use it in GitHub Desktop.
Save showaltb/0456ce0002842c88c3fc06db43f3ee7b to your computer and use it in GitHub Desktop.
RSpec become, become_truthy, become_falsey matchers
# Matchers that will wait for a value to change.
# Tested with Ruby 2.3, RSpec 3.3
# Ex. expect { email.reload.delivered? }.to become_truthy
RSpec::Matchers.define :become_truthy do
supports_block_expectations
match do |block|
begin
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = block.call
value
end
rescue Timeout::Error
false
end
end
end
RSpec::Matchers.define :become_falsey do
supports_block_expectations
match do |block|
begin
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = !block.call
value
end
rescue Timeout::Error
false
end
end
end
# Ex. expect { page.current_url }.to become( '/#/something_or_other' )
RSpec::Matchers.define :become do |expected|
supports_block_expectations
match do |block|
begin
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = ( block.call == expected )
value
end
rescue Timeout::Error
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment