Question

For one of my integration tests (minitest::spec) for my Rails app, I want to assert the number of times a bit of text shows up on a webpage? For instance I want to make sure that the text "This bit of text." only shows up once on the page. Is there an additional parameter that I can send to page.must_have_content?

page.must_have_content('This bit of text.')

Or some other type of assertion? Thanks.

Was it helpful?

Solution

You can use the options :count, :between, :minimum or :maximum to specify the number of matches expected.

The :count option specifies an exact number of matches. For example, the following requires the page to have the text exactly once:

page.must_have_content('This bit of text.', :count => 1)

The :between option specifies a range of matches expected. For example, the following expects the page to have between 2 to 4 matches:

page.must_have_content('This bit of text.', :between => 2..4)

The :minimum option specifies the minimum number of matches required. For example, the following requires there to be 2 or more matches.

page.must_have_content('This bit of text.', :minimum => 2)

The :maximum option specifies the maximum number of matches allowed. For example, the following expects there to be at most 2 matches.

page.must_have_content('This bit of text.', :maximum => 2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top