Phil
Phil

Reputation: 916

Capybara warns of "checking for expected text of nil"

When I'm running tests, I happen to see this in my terminal window for a specific feature spec that I'm running:

Checking for expected text of nil is confusing and/or pointless since it will always match.
Please specify a string or regexp instead.

I don't believe that my code has any improper use of nil so I'm wondering what it means. I'm also not using nil in any rspec expectations or capybara methods. Is there a way for me to help troubleshoot this message further?

Upvotes: 1

Views: 1016

Answers (2)

Phil
Phil

Reputation: 916

I made a mistake in using an instance variable, something like page.should have_text @foo, but I had not used or initalized @foo anywhere. Bad mistake on my part.

Upvotes: 1

sugaryourcoffee
sugaryourcoffee

Reputation: 879

This is caused by a variable that is nil while checking e.g. expect(page).to have_text user.middle_name where in this case user.middle_name is nil because the user has no middle name.

The warn message is issued in your "gems-directory"/gems/capybara-x.x.x/lib/capybara/queries/text_query.rb.

To find out where the nil check occurs add raise "here I check for nil" before the warn.

You will get a runtime error with the line where it occurs in your spec.

RuntimeError
  here I check for nil
# ... stack trace omitted
# ./spec/requests/user_show_spec.rb:45:in `block (2 levels) in <top (required)>

Upvotes: 2

Related Questions