Ensure not_to.have_xxx and to.have_no_xxx error messages are identical

This commit is contained in:
Thomas Walpole 2018-02-28 12:54:18 -08:00
parent 7491e0606f
commit 66fd3f0edf
3 changed files with 51 additions and 1 deletions

View File

@ -199,6 +199,32 @@ module Capybara
end
end
class NegatedMatcher
def initialize(matcher)
@matcher = matcher
end
def matches?(actual)
@matcher.does_not_match?(actual)
end
def does_not_match?(actual)
@matcher.matches?(actual)
end
def description
"not #{@matcher.description}"
end
def failure_message
@matcher.failure_message_when_negated
end
def failure_message_when_negated
@matcher.failure_message
end
end
class BecomeClosed
def initialize(options)
@options = options
@ -279,6 +305,7 @@ module Capybara
end
alias_method :have_content, :have_text
def have_title(title, **options)
HaveTitle.new(title, options)
end
@ -331,6 +358,13 @@ module Capybara
HaveSelector.new(:table, locator, options, &optional_filter_block)
end
%w(selector css xpath text title current_path link button field checked_field unchecked_field select table).each do |matcher_type|
define_method "have_no_#{matcher_type}" do |*args, &optional_filter_block|
NegatedMatcher.new(send("have_#{matcher_type}", *args, &optional_filter_block))
end
end
alias_method :have_no_content, :have_no_text
##
# Wait for window to become closed.
# @example

View File

@ -252,7 +252,7 @@ Capybara::SpecHelper.spec '#has_no_text?' do
expect(@session).not_to have_no_text('exercitation ullamco laboris')
end
it "should be true if the given text is not on the page" do
it "should be true if the given text is not on the page", :focus_ do
@session.visit('/with_html')
expect(@session).to have_no_text('xxxxyzzz')
expect(@session).to have_no_text('monkey')

View File

@ -16,6 +16,18 @@ RSpec.describe 'Capybara RSpec Matchers', :type => :feature do
expect(page).to matcher
expect { matcher.description }.not_to raise_error
end
it "should produce the same error for .to have_no_xxx and .not_to have_xxx" do
visit('/with_html')
not_to_msg = error_msg_for { expect(page).not_to have_selector(:css, '#referrer') }
have_no_msg = error_msg_for { expect(page).to have_no_selector(:css, '#referrer') }
expect(not_to_msg).to eq have_no_msg
not_to_msg = error_msg_for { expect(page).not_to have_text('This is a test') }
have_no_msg = error_msg_for { expect(page).to have_no_text('This is a test') }
puts not_to_msg
expect(not_to_msg).to eq have_no_msg
end
end
context "after called on element" do
@ -43,4 +55,8 @@ RSpec.describe 'Capybara RSpec Matchers', :type => :feature do
expect { matcher.description }.not_to raise_error
end
end
def error_msg_for(&block)
expect(&block).to raise_error { |e| return e.message }
end
end