mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
initial stab at have_selector matcher for Strings
This commit is contained in:
parent
8c301cedb1
commit
1e7c79ffc0
2 changed files with 62 additions and 0 deletions
|
@ -16,3 +16,34 @@ RSpec.configure do |config|
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Capybara
|
||||
module RSpec
|
||||
module StringMatchers
|
||||
extend ::RSpec::Matchers::DSL
|
||||
|
||||
matcher :have_selector do |*args|
|
||||
match_for_should do |string|
|
||||
Capybara::string(string).has_selector?(*args)
|
||||
end
|
||||
|
||||
match_for_should_not do |string|
|
||||
Capybara::string(string).has_no_selector?(*args)
|
||||
end
|
||||
|
||||
failure_message_for_should do |string|
|
||||
"expected selector #{formatted(args)} to return something from:\n#{string}"
|
||||
end
|
||||
|
||||
failure_message_for_should_not do |string|
|
||||
"expected selector #{formatted(args)} not to return anything from:\n#{string}"
|
||||
end
|
||||
|
||||
def formatted(args)
|
||||
args.length == 1 ? args.first.inspect : args.inspect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,3 +45,34 @@ describe 'capybara/rspec', :type => :other do
|
|||
expect { visit('/') }.to raise_error(NoMethodError)
|
||||
end
|
||||
end
|
||||
|
||||
describe Capybara::RSpec::StringMatchers do
|
||||
include Capybara::RSpec::StringMatchers
|
||||
before { Capybara.default_selector = :css }
|
||||
after { Capybara.default_selector = :xpath }
|
||||
describe "have_selector matcher" do
|
||||
context "with should" do
|
||||
it "passes if the String has_selector?" do
|
||||
"<h1>Text</h1>".should have_selector('h1')
|
||||
end
|
||||
|
||||
it "fails if the String returns false from has_selector?" do
|
||||
expect do
|
||||
"<h1>Text</h1>".should have_selector('h2')
|
||||
end.to raise_error(/expected selector .* to return/)
|
||||
end
|
||||
end
|
||||
|
||||
context "with should_not" do
|
||||
it "passes if has_no_selector? returns true" do
|
||||
"<h1>Text</h1>".should_not have_selector('h2')
|
||||
end
|
||||
|
||||
it "fails if has_no_selector? returns false" do
|
||||
expect do
|
||||
"<h1>Text</h1>".should_not have_selector('h1')
|
||||
end.to raise_error(/expected selector .* not to return/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue