capybara_minitest_spec 0.2.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. data/.rvmrc +1 -1
  2. data/HISTORY.md +0 -0
  3. data/README.md +31 -32
  4. data/Rakefile +2 -2
  5. data/capybara_minitest_spec.gemspec +6 -4
  6. data/lib/capybara_minitest_spec.rb +53 -6
  7. data/lib/capybara_minitest_spec/test_name.rb +67 -0
  8. data/lib/capybara_minitest_spec/version.rb +1 -1
  9. data/spec/capybara_minitest_spec.spec.rb +13 -0
  10. data/spec/capybara_rspec_matchers.spec.rb +580 -0
  11. data/spec/helper.rb +39 -0
  12. data/{test_app → spec/test_app}/test_app.rb +0 -0
  13. data/{test_app → spec/test_app}/views/buttons.erb +0 -0
  14. data/{test_app → spec/test_app}/views/fieldsets.erb +0 -0
  15. data/{test_app → spec/test_app}/views/form.erb +0 -0
  16. data/{test_app → spec/test_app}/views/frame_one.erb +0 -0
  17. data/{test_app → spec/test_app}/views/frame_two.erb +0 -0
  18. data/{test_app → spec/test_app}/views/header_links.erb +0 -0
  19. data/{test_app → spec/test_app}/views/host_links.erb +0 -0
  20. data/{test_app → spec/test_app}/views/popup_one.erb +0 -0
  21. data/{test_app → spec/test_app}/views/popup_two.erb +0 -0
  22. data/{test_app → spec/test_app}/views/postback.erb +0 -0
  23. data/{test_app → spec/test_app}/views/tables.erb +0 -0
  24. data/{test_app → spec/test_app}/views/with_html.erb +0 -0
  25. data/{test_app → spec/test_app}/views/with_html_entities.erb +0 -0
  26. data/{test_app → spec/test_app}/views/with_js.erb +0 -0
  27. data/{test_app → spec/test_app}/views/with_scope.erb +0 -0
  28. data/{test_app → spec/test_app}/views/with_simple_html.erb +0 -0
  29. data/{test_app → spec/test_app}/views/within_frames.erb +0 -0
  30. data/{test_app → spec/test_app}/views/within_popups.erb +0 -0
  31. data/spec/test_name.spec.rb +47 -0
  32. metadata +142 -121
  33. data/lib/capybara_minitest_spec/matcher.rb +0 -126
  34. data/test/capybara_node_matchers_spec.rb +0 -471
  35. data/test/custom_matcher_spec.rb +0 -29
  36. data/test/matcher_spec.rb +0 -45
  37. data/test/spec_helper.rb +0 -14
  38. data/test_app/driver.rb +0 -297
  39. data/test_app/fixtures/capybara.jpg +0 -3
  40. data/test_app/fixtures/test_file.txt +0 -1
  41. data/test_app/public/test.js +0 -43
  42. data/test_app/session.rb +0 -154
  43. data/test_app/session/all_spec.rb +0 -78
  44. data/test_app/session/attach_file_spec.rb +0 -73
  45. data/test_app/session/check_spec.rb +0 -65
  46. data/test_app/session/choose_spec.rb +0 -26
  47. data/test_app/session/click_button_spec.rb +0 -304
  48. data/test_app/session/click_link_or_button_spec.rb +0 -36
  49. data/test_app/session/click_link_spec.rb +0 -119
  50. data/test_app/session/current_host_spec.rb +0 -68
  51. data/test_app/session/current_url_spec.rb +0 -15
  52. data/test_app/session/fill_in_spec.rb +0 -125
  53. data/test_app/session/find_button_spec.rb +0 -18
  54. data/test_app/session/find_by_id_spec.rb +0 -18
  55. data/test_app/session/find_field_spec.rb +0 -26
  56. data/test_app/session/find_link_spec.rb +0 -19
  57. data/test_app/session/find_spec.rb +0 -149
  58. data/test_app/session/first_spec.rb +0 -105
  59. data/test_app/session/has_button_spec.rb +0 -32
  60. data/test_app/session/has_content_spec.rb +0 -106
  61. data/test_app/session/has_css_spec.rb +0 -243
  62. data/test_app/session/has_field_spec.rb +0 -192
  63. data/test_app/session/has_link_spec.rb +0 -37
  64. data/test_app/session/has_select_spec.rb +0 -129
  65. data/test_app/session/has_selector_spec.rb +0 -129
  66. data/test_app/session/has_table_spec.rb +0 -96
  67. data/test_app/session/has_xpath_spec.rb +0 -123
  68. data/test_app/session/headers.rb +0 -19
  69. data/test_app/session/javascript.rb +0 -289
  70. data/test_app/session/response_code.rb +0 -19
  71. data/test_app/session/select_spec.rb +0 -113
  72. data/test_app/session/text_spec.rb +0 -19
  73. data/test_app/session/uncheck_spec.rb +0 -21
  74. data/test_app/session/unselect_spec.rb +0 -61
  75. data/test_app/session/within_spec.rb +0 -178
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use --create ruby-1.9.2-p0@capybara_minispec
1
+ rvm use --create ruby-1.9.3-p327@capybara_minispec
File without changes
data/README.md CHANGED
@@ -1,56 +1,55 @@
1
- # Capybara MiniTest Spec [![Build Status](https://secure.travis-ci.org/ordinaryzelig/capybara_minitest_spec.png?branch=master)](http://travis-ci.org/ordinaryzelig/capybara_minitest_spec)
1
+ CapybaraMiniTestSpec
2
+ ====================
2
3
 
3
- Define MiniTest::Spec expectations for Capybara node matchers.
4
+ [![Build Status](https://secure.travis-ci.org/ordinaryzelig/capybara_minitest_spec.png?branch=master)](http://travis-ci.org/ordinaryzelig/capybara_minitest_spec)
4
5
 
5
6
  ## Description
6
7
 
7
- If you like using Capybara and RSpec, you're used to matchers like:
8
+ If you've used [Capybara](https://github.com/jnicklas/capybara) and RSpec, you've probably used matchers like:
8
9
 
9
- page.should have_content('Title')
10
+ ```ruby
11
+ page.should have_content('Title')
12
+ ```
10
13
 
11
- Now you can have similar functionality while using MiniTest::Spec:
14
+ With CapybaraMiniTestSpec you can have similar functionality while using MiniTest::Spec:
12
15
 
13
- page.must_have_content('Title')
16
+ ```ruby
17
+ page.must_have_content('Title')
18
+ ```
14
19
 
15
- ## Install
20
+ You can also use the TestUnit style assertions:
16
21
 
17
- gem install capybara_minitest_spec
22
+ ```ruby
23
+ assert_page_has_content page, 'Title'
24
+ ```
18
25
 
19
- ## Usage
26
+ But if you really want to be simple with MiniTest::Unit, you don't need this gem.
27
+ You can do the following without this gem:
20
28
 
21
- The gem iterates through all of Capybara::Node's matcher methods and defines these things for each one:
29
+ ```ruby
30
+ assert page.has_content?('Title'), 'Your custom failure message'
31
+ ```
22
32
 
23
- * positive assertion: assert_page_has_css(page, 'h1')
24
- * positive expectation: page.must_have_css('h1')
25
- * negative assertion: refute_page_has_css(page, 'h1')
26
- * negative expectation: page.wont_have_css('h1')
33
+ However, if you choose to use this gem, you get Capybara's failure messages as if you were using RSpec matchers.
27
34
 
28
- ### Custom Expectations
35
+ ```ruby
36
+ assert_page_has_content?('<h1>Content</h1>', 'No such Text')
37
+ # fails with 'expected there to be text "No such Text" in "Content"'
38
+ ```
29
39
 
30
- If you want to add your own custom page expectations, it's easy.
40
+ ## Install
31
41
 
32
42
  ```ruby
33
- # First define a Capybara session method so that
34
- # page.has_flash_message?(message) is available.
35
- class Capybara::Session
36
- def has_flash_message?(message)
37
- within '#flash' do
38
- has_content? message
39
- end
40
- end
41
- end
42
- # Then create a new CapybaraMiniTestSpec::Matcher.
43
- CapybaraMiniTestSpec::Matcher.new(:has_flash_message?)
43
+ # Gemfile
44
+ gem 'capybara_minitest_spec'
44
45
  ```
45
46
 
46
- Now you can do `page.must_have_flash_message('Successfully created')`
47
+ NOTE: If after installing the Capybara gem, Nokogiri isn't installed, it's a known bug (https://github.com/jnicklas/capybara/issues/882).
47
48
 
48
49
  ## Compatibility
49
50
 
50
- In theory, this should work with any version of Capybara, so the runtime dependency is not version specific. At the time of this writing, it was tested with Capybara 1.1.1.
51
-
51
+ In theory, this should work with Capybara >= 2. At the time of this writing, it was tested with Capybara 2.0.1.
52
52
 
53
53
  ## Testing
54
54
 
55
- This gem was tested by basically copying the Capybara spec located in 'spec/rspec/matchers_spec.rb' and altering the test to run with MiniTest.
56
- Capybara uses xpath as a submodule of the git repository. I used the regular gem.
55
+ This gem was tested by basically copying the Capybara spec located in 'spec/rspec/matchers_spec.rb' and [altering the test to run with MiniTest](https://gist.github.com/4297afa19edd44885248).
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require "bundler/gem_tasks"
2
2
  require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
- t.libs << 'test'
6
- t.pattern = 'test/*_spec.rb'
5
+ t.libs << 'spec'
6
+ t.pattern = 'spec/*.spec.rb'
7
7
  end
8
8
 
9
9
  task :default => :test
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Jared Ning"]
9
9
  s.email = ["jared@redningja.com"]
10
10
  s.homepage = "https://github.com/ordinaryzelig/capybara_minitest_spec"
11
- s.summary = %q{MiniTest::Spec expectations for Capybara node matchers.}
12
- s.description = %q{MiniTest::Spec expectations for Capybara node matchers.}
11
+ s.summary = %q{Capybara + MiniTest::Spec}
12
+ s.description = %q{Use Capybara matchers with MiniTest. Specifically, it defines MiniTest::Spec expectations like page.must_have_content('content').}
13
13
 
14
14
  s.rubyforge_project = "capybara_minitest_spec"
15
15
 
@@ -18,8 +18,10 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_runtime_dependency 'capybara'
22
- s.add_runtime_dependency 'minitest', '>= 2.0'
21
+ s.add_runtime_dependency 'capybara', '>= 2'
22
+ s.add_runtime_dependency 'minitest', '>= 2'
23
23
 
24
+ s.add_development_dependency 'awesome_print'
25
+ s.add_development_dependency 'rake'
24
26
  s.add_development_dependency 'sinatra', '>= 0.9.4'
25
27
  end
@@ -1,11 +1,58 @@
1
+ require "capybara_minitest_spec/version"
2
+
1
3
  require 'minitest/spec'
2
4
  require 'capybara'
3
- require "capybara_minitest_spec/matcher"
4
- require "capybara_minitest_spec/version"
5
+ require 'capybara/rspec/matchers'
6
+ require "capybara_minitest_spec/test_name"
7
+
8
+ module CapybaraMiniTestSpec
9
+
10
+ module_function
11
+
12
+ # Define assertions and spec expectations for both positive and negative forms of the matcher name.
13
+ def add_matcher(matcher_name)
14
+ positive_name = CapybaraMiniTestSpec::PositiveTestName.new(matcher_name)
15
+ negative_name = CapybaraMiniTestSpec::NegativeTestName.new(matcher_name)
16
+ [positive_name, negative_name].each do |test_name|
17
+ CapybaraMiniTestSpec.define_expectation(test_name)
18
+ end
19
+ end
20
+
21
+ def define_expectation(test_name)
22
+ define_assertion(test_name)
23
+ infect_assertion(test_name)
24
+ end
25
+
26
+ # Define an assertion using test_name.
27
+ # For example, if the test name is have_css,
28
+ # the assertion would be called assert_page_has_css.
29
+ #
30
+ # Use either of Capybara::RSpecMatchers.
31
+ # Assert that it matches, and pass its failure message if it doesn't.
32
+ def define_assertion(test_name)
33
+ method_name = test_name.assertion_name
34
+ MiniTest::Assertions.send :define_method, method_name do |page, *args|
35
+ matcher = test_name.matcher(*args)
36
+
37
+ matches = matcher.send(test_name.match_method, page)
38
+ failure_message = message do
39
+ matcher.send(test_name.failure_message_method)
40
+ end
41
+
42
+ assert matches, failure_message
43
+ end
44
+ end
45
+
46
+ # Define the MiniTest::Spec expectation.
47
+ def infect_assertion(test_name)
48
+ MiniTest::Expectations.infect_an_assertion test_name.assertion_name, test_name.expectation_name, true
49
+ end
50
+
51
+ end
5
52
 
6
- # For each Capybara node matcher,
53
+ # For each Capybara RSpec matcher name,
7
54
  # define positive and negative MiniTest::Unit assertions and
8
- # infect it both assertions to create MiniTest::Spec expectations.
9
- Capybara::Node::Matchers.public_instance_methods.each do |matcher_name|
10
- CapybaraMiniTestSpec::Matcher.new(matcher_name)
55
+ # infect both assertions to create MiniTest::Spec expectations.
56
+ Capybara::RSpecMatchers.public_instance_methods.each do |matcher_name|
57
+ CapybaraMiniTestSpec.add_matcher(matcher_name)
11
58
  end
@@ -0,0 +1,67 @@
1
+ module CapybaraMiniTestSpec
2
+ # Represents the a matcher name.
3
+ # Returns different forms of the name depending on whether it is positive or negative.
4
+ class TestName
5
+
6
+ include Capybara::RSpecMatchers
7
+
8
+ def initialize(name)
9
+ @original_name = name.to_s
10
+ end
11
+
12
+ def matcher(*args)
13
+ send @original_name, *args
14
+ end
15
+
16
+ private
17
+
18
+ def has
19
+ have.sub /^have/, 'has'
20
+ end
21
+
22
+ def have
23
+ @original_name
24
+ end
25
+
26
+ end
27
+
28
+ class PositiveTestName < TestName
29
+
30
+ def assertion_name
31
+ "assert_page_#{has}"
32
+ end
33
+
34
+ def expectation_name
35
+ "must_#{have}"
36
+ end
37
+
38
+ def match_method
39
+ :matches?
40
+ end
41
+
42
+ def failure_message_method
43
+ :failure_message_for_should
44
+ end
45
+
46
+ end
47
+
48
+ class NegativeTestName < TestName
49
+
50
+ def assertion_name
51
+ "refute_page_#{has}"
52
+ end
53
+
54
+ def expectation_name
55
+ "wont_#{have}"
56
+ end
57
+
58
+ def failure_message_method
59
+ :failure_message_for_should_not
60
+ end
61
+
62
+ def match_method
63
+ :does_not_match?
64
+ end
65
+
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module CapybaraMiniTestSpec
2
- VERSION = '0.2.2'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -0,0 +1,13 @@
1
+ require_relative 'helper'
2
+
3
+ describe CapybaraMiniTestSpec do
4
+
5
+ it 'defines the assertion' do
6
+ MiniTest::Assertions.public_instance_methods.must_include :assert_page_has_css
7
+ end
8
+
9
+ it 'defines the spec expectation' do
10
+ MiniTest::Expectations.public_instance_methods.must_include :must_have_css
11
+ end
12
+
13
+ end
@@ -0,0 +1,580 @@
1
+ require_relative 'helper'
2
+
3
+ # Setup Rack test app.
4
+ require_relative 'test_app/test_app'
5
+ Capybara.app = TestApp
6
+
7
+
8
+ describe CapybaraMiniTestSpec do
9
+ include Capybara::DSL
10
+ include Capybara::RSpecMatchers
11
+
12
+ describe "have_css matcher" do
13
+ it "gives proper description" do
14
+ have_css('h1').description.must_equal "have css \"h1\""
15
+ end
16
+
17
+ context "on a string" do
18
+ context "with should" do
19
+ it "passes if has_css? returns true" do
20
+ "<h1>Text</h1>".must_have_css('h1')
21
+ end
22
+
23
+ it "fails if has_css? returns false" do
24
+ expect do
25
+ "<h1>Text</h1>".must_have_css('h2')
26
+ end.must_raise_with_message(/expected to find css "h2" but there were no matches/)
27
+ end
28
+
29
+ it "passes if matched node count equals expected count" do
30
+ "<h1>Text</h1>".must_have_css('h1', :count => 1)
31
+ end
32
+
33
+ it "fails if matched node count does not equal expected count" do
34
+ expect do
35
+ "<h1>Text</h1>".must_have_css('h1', :count => 2)
36
+ end.must_raise_with_message(/expected css "h1" to be found 2 times/)
37
+ end
38
+ end
39
+
40
+ context "with should_not" do
41
+ it "passes if has_no_css? returns true" do
42
+ "<h1>Text</h1>".wont_have_css('h2')
43
+ end
44
+
45
+ it "fails if has_no_css? returns false" do
46
+ expect do
47
+ "<h1>Text</h1>".wont_have_css('h1')
48
+ end.must_raise_with_message(/expected not to find css "h1"/)
49
+ end
50
+
51
+ it "passes if matched node count does not equal expected count" do
52
+ "<h1>Text</h1>".wont_have_css('h1', :count => 2)
53
+ end
54
+
55
+ it "fails if matched node count equals expected count" do
56
+ expect do
57
+ "<h1>Text</h1>".wont_have_css('h1', :count => 1)
58
+ end.must_raise_with_message(/expected not to find css "h1"/)
59
+ end
60
+ end
61
+ end
62
+
63
+ context "on a page or node" do
64
+ before do
65
+ visit('/with_html')
66
+ end
67
+
68
+ context "with should" do
69
+ it "passes if has_css? returns true" do
70
+ page.must_have_css('h1')
71
+ end
72
+
73
+ it "fails if has_css? returns false" do
74
+ expect do
75
+ page.must_have_css('h1#doesnotexist')
76
+ end.must_raise_with_message(/expected to find css "h1#doesnotexist" but there were no matches/)
77
+ end
78
+ end
79
+
80
+ context "with should_not" do
81
+ it "passes if has_no_css? returns true" do
82
+ page.wont_have_css('h1#doesnotexist')
83
+ end
84
+
85
+ it "fails if has_no_css? returns false" do
86
+ expect do
87
+ page.wont_have_css('h1')
88
+ end.must_raise_with_message(/expected not to find css "h1"/)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "have_xpath matcher" do
95
+ it "gives proper description" do
96
+ have_xpath('//h1').description.must_equal "have xpath \"\/\/h1\""
97
+ end
98
+
99
+ context "on a string" do
100
+ context "with should" do
101
+ it "passes if has_xpath? returns true" do
102
+ "<h1>Text</h1>".must_have_xpath('//h1')
103
+ end
104
+
105
+ it "fails if has_xpath? returns false" do
106
+ expect do
107
+ "<h1>Text</h1>".must_have_xpath('//h2')
108
+ end.must_raise_with_message(%r(expected to find xpath "//h2" but there were no matches))
109
+ end
110
+ end
111
+
112
+ context "with should_not" do
113
+ it "passes if has_no_xpath? returns true" do
114
+ "<h1>Text</h1>".wont_have_xpath('//h2')
115
+ end
116
+
117
+ it "fails if has_no_xpath? returns false" do
118
+ expect do
119
+ "<h1>Text</h1>".wont_have_xpath('//h1')
120
+ end.must_raise_with_message(%r(expected not to find xpath "//h1"))
121
+ end
122
+ end
123
+ end
124
+
125
+ context "on a page or node" do
126
+ before do
127
+ visit('/with_html')
128
+ end
129
+
130
+ context "with should" do
131
+ it "passes if has_xpath? returns true" do
132
+ page.must_have_xpath('//h1')
133
+ end
134
+
135
+ it "fails if has_xpath? returns false" do
136
+ expect do
137
+ page.must_have_xpath("//h1[@id='doesnotexist']")
138
+ end.must_raise_with_message(%r(expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches))
139
+ end
140
+ end
141
+
142
+ context "with should_not" do
143
+ it "passes if has_no_xpath? returns true" do
144
+ page.wont_have_xpath('//h1[@id="doesnotexist"]')
145
+ end
146
+
147
+ it "fails if has_no_xpath? returns false" do
148
+ expect do
149
+ page.wont_have_xpath('//h1')
150
+ end.must_raise_with_message(%r(expected not to find xpath "//h1"))
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ describe "have_selector matcher" do
157
+ it "gives proper description" do
158
+ matcher = have_selector('//h1')
159
+ "<h1>Text</h1>".must_have_selector('//h1')
160
+ matcher.description.must_equal "have xpath \"//h1\""
161
+ end
162
+
163
+ context "on a string" do
164
+ context "with should" do
165
+ it "passes if has_selector? returns true" do
166
+ "<h1>Text</h1>".must_have_selector('//h1')
167
+ end
168
+
169
+ it "fails if has_selector? returns false" do
170
+ expect do
171
+ "<h1>Text</h1>".must_have_selector('//h2')
172
+ end.must_raise_with_message(%r(expected to find xpath "//h2" but there were no matches))
173
+ end
174
+ end
175
+
176
+ context "with should_not" do
177
+ it "passes if has_no_selector? returns true" do
178
+ "<h1>Text</h1>".wont_have_selector(:css, 'h2')
179
+ end
180
+
181
+ it "fails if has_no_selector? returns false" do
182
+ expect do
183
+ "<h1>Text</h1>".wont_have_selector(:css, 'h1')
184
+ end.must_raise_with_message(%r(expected not to find css "h1"))
185
+ end
186
+ end
187
+ end
188
+
189
+ context "on a page or node" do
190
+ before do
191
+ visit('/with_html')
192
+ end
193
+
194
+ context "with should" do
195
+ it "passes if has_selector? returns true" do
196
+ page.must_have_selector('//h1', :text => 'test')
197
+ end
198
+
199
+ it "fails if has_selector? returns false" do
200
+ expect do
201
+ page.must_have_selector("//h1[@id='doesnotexist']")
202
+ end.must_raise_with_message(%r(expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches))
203
+ end
204
+
205
+ it "includes text in error message" do
206
+ expect do
207
+ page.must_have_selector("//h1", :text => 'wrong text')
208
+ end.must_raise_with_message(%r(expected to find xpath "//h1" with text "wrong text" but there were no matches))
209
+ end
210
+ end
211
+
212
+ context "with should_not" do
213
+ it "passes if has_no_css? returns true" do
214
+ page.wont_have_selector(:css, 'h1#doesnotexist')
215
+ end
216
+
217
+ it "fails if has_no_selector? returns false" do
218
+ expect do
219
+ page.wont_have_selector(:css, 'h1', :text => 'test')
220
+ end.must_raise_with_message(%r(expected not to find css "h1" with text "test"))
221
+ end
222
+ end
223
+ end
224
+ end
225
+
226
+ describe "have_content matcher" do
227
+ it "gives proper description" do
228
+ have_content('Text').description.must_equal "have text \"Text\""
229
+ end
230
+
231
+ context "on a string" do
232
+ context "with should" do
233
+ it "passes if has_content? returns true" do
234
+ "<h1>Text</h1>".must_have_content('Text')
235
+ end
236
+
237
+ it "passes if has_content? returns true using regexp" do
238
+ "<h1>Text</h1>".must_have_content(/ext/)
239
+ end
240
+
241
+ it "fails if has_content? returns false" do
242
+ expect do
243
+ "<h1>Text</h1>".must_have_content('No such Text')
244
+ end.must_raise_with_message(/expected there to be text "No such Text" in "Text"/)
245
+ end
246
+ end
247
+
248
+ context "with should_not" do
249
+ it "passes if has_no_content? returns true" do
250
+ "<h1>Text</h1>".wont_have_content('No such Text')
251
+ end
252
+
253
+ it "passes because escapes any characters that would have special meaning in a regexp" do
254
+ "<h1>Text</h1>".wont_have_content('.')
255
+ end
256
+
257
+ it "fails if has_no_content? returns false" do
258
+ expect do
259
+ "<h1>Text</h1>".wont_have_content('Text')
260
+ end.must_raise_with_message(/expected there not to be text "Text" in "Text"/)
261
+ end
262
+ end
263
+ end
264
+
265
+ context "on a page or node" do
266
+ before do
267
+ visit('/with_html')
268
+ end
269
+
270
+ context "with should" do
271
+ it "passes if has_content? returns true" do
272
+ page.must_have_content('This is a test')
273
+ end
274
+
275
+ it "passes if has_content? returns true using regexp" do
276
+ page.must_have_content(/test/)
277
+ end
278
+
279
+ it "fails if has_content? returns false" do
280
+ expect do
281
+ page.must_have_content('No such Text')
282
+ end.must_raise_with_message(/expected there to be text "No such Text" in "(.*)This is a test(.*)"/)
283
+ end
284
+
285
+ context "with default selector CSS" do
286
+ before { Capybara.default_selector = :css }
287
+ it "fails if has_content? returns false" do
288
+ expect do
289
+ page.must_have_content('No such Text')
290
+ end.must_raise_with_message(/expected there to be text "No such Text" in "(.*)This is a test(.*)"/)
291
+ end
292
+ after { Capybara.default_selector = :xpath }
293
+ end
294
+ end
295
+
296
+ context "with should_not" do
297
+ it "passes if has_no_content? returns true" do
298
+ page.wont_have_content('No such Text')
299
+ end
300
+
301
+ it "fails if has_no_content? returns false" do
302
+ expect do
303
+ page.wont_have_content('This is a test')
304
+ end.must_raise_with_message(/expected there not to be text "This is a test"/)
305
+ end
306
+ end
307
+ end
308
+ end
309
+
310
+ describe "have_text matcher" do
311
+ it "gives proper description" do
312
+ have_text('Text').description.must_equal "have text \"Text\""
313
+ end
314
+
315
+ context "on a string" do
316
+ context "with should" do
317
+ it "passes if has_text? returns true" do
318
+ "<h1>Text</h1>".must_have_text('Text')
319
+ end
320
+
321
+ it "passes if has_text? returns true using regexp" do
322
+ "<h1>Text</h1>".must_have_text(/ext/)
323
+ end
324
+
325
+ it "fails if has_text? returns false" do
326
+ expect do
327
+ "<h1>Text</h1>".must_have_text('No such Text')
328
+ end.must_raise_with_message(/expected there to be text "No such Text" in "Text"/)
329
+ end
330
+
331
+ it "casts has_text? argument to string" do
332
+ expect do
333
+ "<h1>Text</h1>".must_have_text(:cast_me)
334
+ end.must_raise_with_message(/expected there to be text "cast_me" in "Text"/)
335
+ end
336
+ end
337
+
338
+ context "with should_not" do
339
+ it "passes if has_no_text? returns true" do
340
+ "<h1>Text</h1>".wont_have_text('No such Text')
341
+ end
342
+
343
+ it "passes because escapes any characters that would have special meaning in a regexp" do
344
+ "<h1>Text</h1>".wont_have_text('.')
345
+ end
346
+
347
+ it "fails if has_no_text? returns false" do
348
+ expect do
349
+ "<h1>Text</h1>".wont_have_text('Text')
350
+ end.must_raise_with_message(/expected there not to be text "Text" in "Text"/)
351
+ end
352
+ end
353
+ end
354
+
355
+ context "on a page or node" do
356
+ before do
357
+ visit('/with_html')
358
+ end
359
+
360
+ context "with should" do
361
+ it "passes if has_text? returns true" do
362
+ page.must_have_text('This is a test')
363
+ end
364
+
365
+ it "passes if has_text? returns true using regexp" do
366
+ page.must_have_text(/test/)
367
+ end
368
+
369
+ it "fails if has_text? returns false" do
370
+ expect do
371
+ page.must_have_text('No such Text')
372
+ end.must_raise_with_message(/expected there to be text "No such Text" in "(.*)This is a test(.*)"/)
373
+ end
374
+
375
+ context "with default selector CSS" do
376
+ before { Capybara.default_selector = :css }
377
+ it "fails if has_text? returns false" do
378
+ expect do
379
+ page.must_have_text('No such Text')
380
+ end.must_raise_with_message(/expected there to be text "No such Text" in "(.*)This is a test(.*)"/)
381
+ end
382
+ after { Capybara.default_selector = :xpath }
383
+ end
384
+ end
385
+
386
+ context "with should_not" do
387
+ it "passes if has_no_text? returns true" do
388
+ page.wont_have_text('No such Text')
389
+ end
390
+
391
+ it "fails if has_no_text? returns false" do
392
+ expect do
393
+ page.wont_have_text('This is a test')
394
+ end.must_raise_with_message(/expected there not to be text "This is a test"/)
395
+ end
396
+ end
397
+ end
398
+ end
399
+
400
+ describe "have_link matcher" do
401
+ let(:html) { '<a href="#">Just a link</a>' }
402
+
403
+ it "gives proper description" do
404
+ have_link('Just a link').description.must_equal "have link \"Just a link\""
405
+ end
406
+
407
+ it "passes if there is such a button" do
408
+ html.must_have_link('Just a link')
409
+ end
410
+
411
+ it "fails if there is no such button" do
412
+ expect do
413
+ html.must_have_link('No such Link')
414
+ end.must_raise_with_message(/expected to find link "No such Link"/)
415
+ end
416
+ end
417
+
418
+ describe "have_button matcher" do
419
+ let(:html) { '<button>A button</button><input type="submit" value="Another button"/>' }
420
+
421
+ it "gives proper description" do
422
+ have_button('A button').description.must_equal "have button \"A button\""
423
+ end
424
+
425
+ it "passes if there is such a button" do
426
+ html.must_have_button('A button')
427
+ end
428
+
429
+ it "fails if there is no such button" do
430
+ expect do
431
+ html.must_have_button('No such Button')
432
+ end.must_raise_with_message(/expected to find button "No such Button"/)
433
+ end
434
+ end
435
+
436
+ describe "have_field matcher" do
437
+ let(:html) { '<p><label>Text field<input type="text"/></label></p>' }
438
+
439
+ it "gives proper description" do
440
+ have_field('Text field').description.must_equal "have field \"Text field\""
441
+ end
442
+
443
+ it "passes if there is such a field" do
444
+ html.must_have_field('Text field')
445
+ end
446
+
447
+ it "fails if there is no such field" do
448
+ expect do
449
+ html.must_have_field('No such Field')
450
+ end.must_raise_with_message(/expected to find field "No such Field"/)
451
+ end
452
+ end
453
+
454
+ describe "have_checked_field matcher" do
455
+ let(:html) do
456
+ '<label>it is checked<input type="checkbox" checked="checked"/></label>
457
+ <label>unchecked field<input type="checkbox"/></label>'
458
+ end
459
+
460
+ it "gives proper description" do
461
+ have_checked_field('it is checked').description.must_equal "have field \"it is checked\""
462
+ end
463
+
464
+ context "with should" do
465
+ it "passes if there is such a field and it is checked" do
466
+ html.must_have_checked_field('it is checked')
467
+ end
468
+
469
+ it "fails if there is such a field but it is not checked" do
470
+ expect do
471
+ html.must_have_checked_field('unchecked field')
472
+ end.must_raise_with_message(/expected to find field "unchecked field"/)
473
+ end
474
+
475
+ it "fails if there is no such field" do
476
+ expect do
477
+ html.must_have_checked_field('no such field')
478
+ end.must_raise_with_message(/expected to find field "no such field"/)
479
+ end
480
+ end
481
+
482
+ context "with should not" do
483
+ it "fails if there is such a field and it is checked" do
484
+ expect do
485
+ html.wont_have_checked_field('it is checked')
486
+ end.must_raise_with_message(/expected not to find field "it is checked"/)
487
+ end
488
+
489
+ it "passes if there is such a field but it is not checked" do
490
+ html.wont_have_checked_field('unchecked field')
491
+ end
492
+
493
+ it "passes if there is no such field" do
494
+ html.wont_have_checked_field('no such field')
495
+ end
496
+ end
497
+ end
498
+
499
+ describe "have_unchecked_field matcher" do
500
+ let(:html) do
501
+ '<label>it is checked<input type="checkbox" checked="checked"/></label>
502
+ <label>unchecked field<input type="checkbox"/></label>'
503
+ end
504
+
505
+ it "gives proper description" do
506
+ have_unchecked_field('unchecked field').description.must_equal "have field \"unchecked field\""
507
+ end
508
+
509
+ context "with should" do
510
+ it "passes if there is such a field and it is not checked" do
511
+ html.must_have_unchecked_field('unchecked field')
512
+ end
513
+
514
+ it "fails if there is such a field but it is checked" do
515
+ expect do
516
+ html.must_have_unchecked_field('it is checked')
517
+ end.must_raise_with_message(/expected to find field "it is checked"/)
518
+ end
519
+
520
+ it "fails if there is no such field" do
521
+ expect do
522
+ html.must_have_unchecked_field('no such field')
523
+ end.must_raise_with_message(/expected to find field "no such field"/)
524
+ end
525
+ end
526
+
527
+ context "with should not" do
528
+ it "fails if there is such a field and it is not checked" do
529
+ expect do
530
+ html.wont_have_unchecked_field('unchecked field')
531
+ end.must_raise_with_message(/expected not to find field "unchecked field"/)
532
+ end
533
+
534
+ it "passes if there is such a field but it is checked" do
535
+ html.wont_have_unchecked_field('it is checked')
536
+ end
537
+
538
+ it "passes if there is no such field" do
539
+ html.wont_have_unchecked_field('no such field')
540
+ end
541
+ end
542
+ end
543
+
544
+ describe "have_select matcher" do
545
+ let(:html) { '<label>Select Box<select></select></label>' }
546
+
547
+ it "gives proper description" do
548
+ have_select('Select Box').description.must_equal "have select box \"Select Box\""
549
+ end
550
+
551
+ it "passes if there is such a select" do
552
+ html.must_have_select('Select Box')
553
+ end
554
+
555
+ it "fails if there is no such select" do
556
+ expect do
557
+ html.must_have_select('No such Select box')
558
+ end.must_raise_with_message(/expected to find select box "No such Select box"/)
559
+ end
560
+ end
561
+
562
+ describe "have_table matcher" do
563
+ let(:html) { '<table><caption>Lovely table</caption></table>' }
564
+
565
+ it "gives proper description" do
566
+ have_table('Lovely table').description.must_equal "have table \"Lovely table\""
567
+ end
568
+
569
+ it "passes if there is such a select" do
570
+ html.must_have_table('Lovely table')
571
+ end
572
+
573
+ it "fails if there is no such select" do
574
+ expect do
575
+ html.must_have_table('No such Table')
576
+ end.must_raise_with_message(/expected to find table "No such Table"/)
577
+ end
578
+ end
579
+ end
580
+