Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara/RSpec -- Testing for multiple possible outcomes with have_content

Tags:

rspec

capybara

I'm new to testing, so bear with me :)

I would like to test for random content on a page. For example, let's test the page for displaying the content either 'foo' or 'bar'.

Something like the below is what I'm trying to achieve

page.should (have_content('foo') || have_content('bar'))

Any ideas?

like image 783
Viet Avatar asked Feb 02 '12 21:02

Viet


1 Answers

I don't know any ready to use Mather for you. But you can bake your own.

using satisfy:

page.should satisfy {|page| page.has_content?('foo') or page.has_content?('bar')}

or simple_matcher

def have_foo_or_bar
  simple_matcher("check content has foo or bar") { |page| page.has_content?('foo') or page.has_content?('bar') }
end

describe page
  it "should have foo or bar" do
    page.should have_foo_or_bar
  end
end

Edit: has_content accepts regex, so you can check for page.should have_content(/foo|bar/)

like image 57
Art Shayderov Avatar answered Oct 15 '22 06:10

Art Shayderov