Domanda

So you can have a test like this:

find(".blah").should have_content("blah blah")

But is there a way to just check if something is in blah?

find(".blah").should have_some_content
È stato utile?

Soluzione

I do not believe there is a Capybara::RSpecMatchers for what you want. However, you could use the underlying Capybara::Node::Matchers.

Try:

find(".blah").has_no_text?.should be_true

If you really want to use the Capybara::RSpecMatchers, you could use have_content with a regex that looks for any non-whitespace character.

find(".blah").should have_content(/[^\s]/)

Altri suggerimenti

It looks like you are using Capybara.

Instead of the style in question, I will write it like this

page.should have_css('.bar', text: 'foo')

I would inspect the return value of find(".blah") and see what it is in the following two scenarios:

  • You know it's there, and something is returned
  • You know it's NOT there and something else (maybe nil?) is returned

Then writing the test just to confirm that, in your case, some elements with that class exist is easy. It's just a matter of knowing the difference between the return values of the .find method when there is and is not something found.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top