Viet
Viet

Reputation: 3387

How to test for multiple possible outcomes with have_content?

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?

Upvotes: 10

Views: 4860

Answers (2)

SerKnight
SerKnight

Reputation: 2642

I had this same question but realized it's probably easier & more resilient to add an if statement to your spec.

it 'can complete my incredible form' do
  visit '/testing_path'
  #...dostuff...

  if object_im_testing.contains_my_condition?
    current_path.should eq('/special_condition_url')
  else
    current_path.should eq('/normal_condition_url')
  end
end

Hope this helps w/ another perspective. Simplest solution is usually the best.

Upvotes: 0

Art Shayderov
Art Shayderov

Reputation: 5110

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/)

Upvotes: 12

Related Questions