Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 51 lines (39 sloc) 0.89 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
require 'capybara'

module HaveTextMatcher
  def have_text(text)
    HaveText.new(text)
  end

  class HaveText
    def initialize(text)
      @text = text
    end

    def in(css)
      @css = css
      self
    end

    def matches?(subject)
      @subject = Capybara.string(subject)

      @subject.has_css?(@css || "*", text: @text)
    end

    def failure_message_for_should
      "expected to find #{@text.inspect} #{within}"
    end

    def failure_message_for_should_not
      "expected not to find #{@text.inspect} #{within}"
    end

    private

    def within
      if @css && @subject.has_css?(@css)
        "within\n#{@subject.find(@css).native}"
      else
        "#{inside} within\n#{@subject.native}"
      end
    end

    def inside
      @css ? "inside #{@css.inspect}" : "anywhere"
    end
  end
end

RSpec.configure do |config|
  config.include HaveTextMatcher
end
Something went wrong with that request. Please try again.