Create a gist now

Instantly share code, notes, and snippets.

require "timeout"
module WaitSteps
extend RSpec::Matchers::DSL
matcher :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
end
@agenteo

in Rspec 3.1 you need to add a:

def supports_block_expectations?
  true
end

to the matcher

@oboxodo

I think become_truthy better reflects this method (semantic taken from Rspec's be_truthy). Here's my version and how I'm using it:

# Usage:
#
# it "should show email for a newly created user" do
#   email = "john@example.com"
#
#   visit new_user_path
#   fill_in "Email", with: email
#   fill_in "Password", with: "secret-password"
#   click_button "Save"
#
#   user = nil
#   expect { user = User.find_by(email: email) }.to become_truthy
#
#   visit user_path(user)
#
#   expect(page).to have_content(email)
# end

RSpec::Matchers.define :become_truthy do |event_name|
  supports_block_expectations

  match do |block|
    begin
      Timeout.timeout(Capybara.default_max_wait_time) do
        sleep(0.05) until value = block.call
        value
      end
    rescue TimeoutError
      false
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment