Module: OpenGraphPlus::Minitest

Defined in:
lib/opengraphplus/minitest.rb

Instance Method Summary collapse

Instance Method Details

#assert_og_tag(html, property, content = nil, message = nil) ⇒ Object

Assert that a specific OpenGraph/Twitter tag is present

Examples:

assert_og_tag(response.body, "og:title")
assert_og_tag(response.body, "og:title", "My Title")

Parameters:

  • html (String)

    The HTML to check

  • property (String)

    The tag property (e.g., “og:title”)

  • content (String, nil) (defaults to: nil)

    Expected content (optional)

  • message (String, nil) (defaults to: nil)

    Custom failure message



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opengraphplus/minitest.rb', line 32

def assert_og_tag(html, property, content = nil, message = nil)
  parser = OpenGraphPlus::Parser.new(html)
  actual_content = parser[property]

  if content.nil?
    message ||= "Expected HTML to have #{property} tag, but it was not found"
    assert !actual_content.nil?, message
  else
    message ||= "Expected #{property} to have content #{content.inspect}, but got #{actual_content.inspect}"
    assert_equal content, actual_content, message
  end
end

#assert_open_graph_tags(html, message = nil) ⇒ Object

Assert that all required OpenGraph tags are present

Examples:

assert_open_graph_tags(response.body)

Parameters:

  • html (String)

    The HTML to check

  • message (String, nil) (defaults to: nil)

    Custom failure message



15
16
17
18
19
# File 'lib/opengraphplus/minitest.rb', line 15

def assert_open_graph_tags(html, message = nil)
  parser = OpenGraphPlus::Parser.new(html)
  message ||= "Expected HTML to have all required OpenGraph tags, but missing: #{parser.errors.join(', ')}"
  assert parser.valid?, message
end

#refute_og_tag(html, property, message = nil) ⇒ Object

Assert that a specific OpenGraph/Twitter tag is NOT present

Examples:

refute_og_tag(response.body, "og:private")

Parameters:

  • html (String)

    The HTML to check

  • property (String)

    The tag property (e.g., “og:title”)

  • message (String, nil) (defaults to: nil)

    Custom failure message



54
55
56
57
58
59
# File 'lib/opengraphplus/minitest.rb', line 54

def refute_og_tag(html, property, message = nil)
  parser = OpenGraphPlus::Parser.new(html)
  actual_content = parser[property]
  message ||= "Expected HTML not to have #{property} tag, but it was found with content #{actual_content.inspect}"
  assert actual_content.nil?, message
end