[Rails] Custom matcher for meta og tags

62 views
Skip to first unread message

Juanito Fatas Huang

unread,
Sep 3, 2013, 10:59:08 PM9/3/13
to rs...@googlegroups.com

Hi I wanna match meta og tags. So I modify a bit the matcher from spree/spree's capybara_ext.rb.

RSpec::Matchers.define :have_meta do |name, expected|
  match do |actual|
    has_css?("meta[property='#{name}'][content='#{expected}']", visible: false)
  end

  failure_message_for_should do |actual|
    actual = first("meta[property='#{name}']")
    if actual
      "expected that meta #{name} would have content='#{expected}' but was '#{actual[:content]}'"
    else
      "expected that meta #{name} would exist with content='#{expected}'"
    end
  end
end

I use it like this (visit the root_path using capybara):

describe "should have meta tags" do
  include ApplicationHelper

  it "checks for home page's meta og:title" do
    expect(page).to have_meta('og:title', page_title)
  end

  it "checks for home page's meta og:description" do
    page.should have_meta('og:description', description)
  end
end

page_title and description are from ApplicationHelper.

However, by running spec I found first("meta[property='#{name}']") is always nil.

Is it good to put this kind of spec in spec/features? or spec/requests?

If this is bad, what do you do if you want to match meta og tags?

Reply all
Reply to author
Forward
0 new messages