msmith1114
msmith1114

Reputation: 3229

Confusion on Capybara "Matchers"

So I usually see around 3-4 different examples when "asserting" text for instance.

The capybara docs even mention more than 1

assert_text('foo')

page.has_text?('bar')

page.should have_content('baz') (Which apparently have_content is the same has have_text)

expect(page).to have_content('boop')

So...thats 4 different ways of checking the same thing. I know the expect is an rspec matcher...but what about the rest? What SHOULD I be using? The Capybara docs unfortunately don't go into the difference between them.

Thanks!

Upvotes: 2

Views: 728

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49880

assert_text('foo') is an assertion generally for use with minitest.

page.has_text?('bar') is just a method that returns true or false. You would use this if you wanted to change behavior based on the presence or lack of presence of text on the page. Not used very often when testing an app since you should know what to expect on the page and not need to perform different behavior based on the content. Can be useful if automating/scraping though.

page.should have_content('baz') (Which apparently have_content is the same has have_text) - As you stated - have_text is an alias for have_content so you can use whichever you prefer. have_content is an RSpec matcher and would only be used when using RSpec. should is the old RSpec syntax which required patching the should method onto every object and has since been superseded by expect

expect(page).to have_content('boop') - current RSpec syntax

So if you're using the minitest test framework use the assert_xxx style methods, if using RSpec you could use either the expect style or the should style, although for anything new you probably want to be using expect style.

Upvotes: 2

Related Questions