Last active
October 20, 2016 02:55
-
-
Save tgaff/5094609 to your computer and use it in GitHub Desktop.
become and become_true matchers --- use instead of Capybara's wait_until
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Matchers that will wait for a value to change. | |
# Ex. expect { email.reload.delivered? }.to become_true | |
RSpec::Matchers.define :become_true do | |
match do |block| | |
begin | |
Timeout.timeout(Capybara.default_wait_time) do | |
sleep(0.1) until value = block.call | |
value | |
end | |
rescue TimeoutError | |
false | |
end | |
end | |
end | |
RSpec::Matchers.define :become_false do | |
match do |block| | |
begin | |
Timeout.timeout(Capybara.default_wait_time) do | |
sleep(0.1) until value = !block.call | |
value | |
end | |
rescue TimeoutError | |
false | |
end | |
end | |
end | |
# Ex. expect { page.current_url }.to become( '/#/something_or_other' ) | |
RSpec::Matchers.define :become do |expected| | |
match do |block| | |
begin | |
Timeout.timeout(Capybara.default_wait_time) do | |
sleep(0.1) until value = ( block.call == expected ) | |
value | |
end | |
rescue TimeoutError | |
false | |
end | |
end | |
end |
n.b. you also have to add the line:
supports_block_expectations
to each matcher for RSpec 3.0+
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wardpenney use a block: