Skip to content

Commit 58c55d6

Browse files
committed
Add spec for have and has? shortcuts
1 parent 2da8084 commit 58c55d6

File tree

8 files changed

+52
-4
lines changed

8 files changed

+52
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Upcoming Changes
22

33
* Add `has?` alias for `has_selector?`.
4+
* Delegate `have` to the RSpec collection matcher when passing an Integer.
45

56
## Capybara Test Helpers 1.0.0 (2020-11-17) ##
67

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ group :test do
2121
gem 'simplecov', '< 0.18'
2222
gem 'webdrivers'
2323

24+
# Test delegation to built-in matchers
25+
gem 'rspec-collection_matchers'
26+
2427
# Generator
2528
gem 'generator_spec', require: false
2629
gem 'rails', require: false

Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
capybara_test_helpers (1.0.0)
4+
capybara_test_helpers (1.0.1)
55
activesupport
66
capybara
77

@@ -181,6 +181,8 @@ GEM
181181
rspec-core (~> 3.9.0)
182182
rspec-expectations (~> 3.9.0)
183183
rspec-mocks (~> 3.9.0)
184+
rspec-collection_matchers (1.2.0)
185+
rspec-expectations (>= 2.99.0.beta1)
184186
rspec-core (3.9.3)
185187
rspec-support (~> 3.9.3)
186188
rspec-expectations (3.9.3)
@@ -267,6 +269,7 @@ DEPENDENCIES
267269
rainbow
268270
rake (~> 12.0)
269271
rspec (~> 3.0)
272+
rspec-collection_matchers
270273
rspec-rails
271274
rubocop
272275
selenium-webdriver (~> 3)

examples/rails_app/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../..
33
specs:
4-
capybara_test_helpers (1.0.0)
4+
capybara_test_helpers (1.0.1)
55
activesupport
66
capybara
77

lib/capybara_test_helpers/assertions.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ def invert_expectation
9393
CapybaraTestHelpers.define_helper_method(self, method_name, target: :page, assertion: true, inject_test_helper: false)
9494
end
9595

96-
alias have have_selector
96+
# Public: Allows to call have_selector with a shorter syntax.
97+
def have(*args, **kwargs, &filter)
98+
if args.first.is_a?(Integer)
99+
::RSpec::CollectionMatchers::Have.new(*args, **kwargs)
100+
else
101+
have_selector(*args, **kwargs, &filter)
102+
end
103+
end
97104

98105
# Public: Allows to check on any input value asynchronously.
99106
def have_value(expected_value, **options)

lib/capybara_test_helpers/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
# Easily write fluent Page Objects for Capybara in Ruby.
44
module CapybaraTestHelpers
5-
VERSION = '1.0.0'
5+
VERSION = '1.0.1'
66
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.feature 'matchers', test_helpers: [:form_page] do
4+
describe 'has? and have' do
5+
before do
6+
form_page.visit_page
7+
end
8+
9+
given(:form) { form_page.get_form }
10+
11+
it 'should not fail when using matchers' do
12+
expect(form.has?(:middle_name_input)).to eq true
13+
expect(form.has?(:middle_name_input, disabled: true)).to eq false
14+
end
15+
16+
it 'should allow using have instead of have_selector' do
17+
form.should.have(:middle_name_input)
18+
expect {
19+
form.should.have(:middle_name_input, disabled: true)
20+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /Expected disabled true but it wasn't/)
21+
end
22+
23+
it 'should be able to use the RSpec matcher' do
24+
expect { expect([1,2,3]).to form.have(3).items }.to raise_error(NameError)
25+
26+
require 'rspec/collection_matchers'
27+
expect([1,2,3]).to form.have(3).items
28+
expect([1,2,3]).not_to form.have(2).items
29+
end
30+
end
31+
end

test_helpers/form_page_test_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class FormPageTestHelper < BaseTestHelper
2424

2525
# Buttons Spec
2626
submit_button: [:button, type: 'submit'],
27+
28+
# Matchers Spec
29+
middle_name_input: [:field, 'Middle Name', disabled: false],
2730
}.freeze
2831

2932
# Getters: A convenient way to get related data or nested elements.

0 commit comments

Comments
 (0)