Rspec: test for specific meta tag content

All I wanted to do was write a couple straightforward Rspec tests for the correct meta tag strings. Simple, right?

Search Hint: Instead of searching on StackOverflow directly, try something like this in Google:
rspec meta tag content
with the Search Tools set to Past Year. You lose some of the relevancy but if you keep running into entries for old versions of gems, it’s worth a try.

Here’s what I ended up with for Rspec 3.1 / Capybara 2.4 (within a Rails 4.1 app). Maybe this will save someone else some time.

One comment. Since the meta tags aren’t part of the user’s activity, like a data entry form would be, their tests don’t belong in a feature. (IMO)

In the Gemfile:

In specs/rails_helper.rb

In controller spec (reports_controller_spec.rb in this example):

Two things to notice. First, the visible: false. The newer versions of Capybara default to true and tags in the <head> aren’t considered visible. (This has been the case for awhile but you might not pick it up from the outdated SO Q/As.) Second, Capybara’s have_title isn’t available in this situation since we’re intentionally not working with a feature.

Thoughts? Leave a comment below.