Sha256: cffcd336cd3a8ea4c55879321d76dbbc87217149d768f932bddffd9e4d71183a

Contents?: true

Size: 890 Bytes

Versions: 21

Compression:

Stored size: 890 Bytes

Contents

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

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/draper-2.1.0/spec/support/matchers/have_text.rb
draper-2.1.0 spec/support/matchers/have_text.rb
draper-2.0.0 spec/support/matchers/have_text.rb
draper-1.4.0 spec/support/matchers/have_text.rb
draper-1.3.1 spec/support/matchers/have_text.rb
strong_presenter-0.2.2 spec/support/matchers/have_text.rb
draper-1.3.0 spec/support/matchers/have_text.rb
strong_presenter-0.2.1 spec/support/matchers/have_text.rb
strong_presenter-0.2.0 spec/support/matchers/have_text.rb
strong_presenter-0.1.0 spec/support/matchers/have_text.rb
draper-1.2.1 spec/support/matchers/have_text.rb
draper-1.2.0 spec/support/matchers/have_text.rb
jamesgolick-draper-1.1.1a spec/support/matchers/have_text.rb
draper-1.1.0 spec/support/matchers/have_text.rb
draper-1.0.0 spec/support/matchers/have_text.rb
draper-1.0.0.beta6 spec/support/matchers/have_text.rb
draper-1.0.0.beta5 spec/support/matchers/have_text.rb
draper-1.0.0.beta4 spec/support/matchers/have_text.rb
draper-1.0.0.beta3 spec/support/matchers/have_text.rb
draper-1.0.0.beta2 spec/support/matchers/have_text.rb