rspec-wait 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19bcbef8a8f87e6d9eb2d9f827bbb8482339bd62
4
- data.tar.gz: 84dbbd7b44d2df67ce8008ca2a081f9f2b9b3325
3
+ metadata.gz: cc0b285ada9260eb2da497cdc35c42d3972cb9ac
4
+ data.tar.gz: 1be11677f7c1f5206dd81fe964abaf1c87d36408
5
5
  SHA512:
6
- metadata.gz: 390f1c7d712b747322ee45c71ca0fad89e34a9dd2eeb19472b4e378aa63773b5705d7a487549ec6a00a2d2880aa64d009b4445d787ca14afa5da99f2e3c07a4e
7
- data.tar.gz: 06f7b337e642e87f7be0bdc525e72bd730333efa5ddc7f9991efdc3b928dc56814d1240b1a22b7ab02e79e1df21118abb7e2de1920d148775abed301332293d2
6
+ metadata.gz: 20d945d658cd08fc1c8429a94972111c61e33a02221fff55182bd6aa90820a7ef921b96e012c3918113f50422a411fa3bf54fcdc5e429b848edff294825ecbe1
7
+ data.tar.gz: ae7062b3a49ae41c62cba4dd79628303db9831d86492f38c2bf6ca362b6db7b1c8848a2978846dd77f8c1b85bf1b6b9984a343fe5bc82794e8f9d40dd4572986
data/.gitignore CHANGED
@@ -1,18 +1,15 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- gemfiles/*.lock
12
- lib/bundler/man
13
- pkg
14
- rdoc
15
- spec/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /gemfiles/*.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --color
2
- --format=documentation
3
- --order=random
2
+ --format documentation
3
+ --order random
4
+ --require spec_helper
@@ -1,3 +1,12 @@
1
+ ## 0.0.4 / 2014-12-18
2
+
3
+ * [FEATURE] Make RSpec::Wait's timeout and delay values configurable
4
+ * [FEATURE] Add the wait(3.seconds).for { something }.to(happen) syntax sugar
5
+
6
+ ## 0.0.3 / 2014-10-29
7
+
8
+ * [ENHANCEMENT] Add support for RSpec 3.2
9
+
1
10
  ## 0.0.2 / 2014-06-11
2
11
 
3
12
  * [ENHANCEMENT] Allow `wait_for` to accept either a value or block target
data/README.md CHANGED
@@ -79,6 +79,54 @@ end
79
79
  RSpec::Wait ties into RSpec's internals so it can take full advantage of any
80
80
  non-block matcher that you would use with RSpec's own `expect` method.
81
81
 
82
+ ### Timeout
83
+
84
+ By default, RSpec::Wait will wait up to 10 seconds for an assertion to pass.
85
+ That timeout value is configurable in three ways:
86
+
87
+ #### RSpec Configuration
88
+
89
+ ```ruby
90
+ RSpec.configure do |config|
91
+ config.wait_timeout = 3 # seconds
92
+ end
93
+ ```
94
+
95
+ #### RSpec Metadata
96
+
97
+ The timeout can also be specified via options added to a spec's or context's
98
+ `:wait` metadata:
99
+
100
+ ```ruby
101
+ scenario "A user can log in successfully", wait: { timeout: 3 } do
102
+ visit new_session_path
103
+
104
+ fill_in "Email", with: "john@example.com"
105
+ fill_in "Password", with: "secret"
106
+ click_button "Log In"
107
+
108
+ wait_for { current_path }.to eq(account_path)
109
+ expect(page).to have_content("Welcome back!")
110
+ end
111
+ ```
112
+
113
+ #### The `wait` Method
114
+
115
+ On a per-assertion basis, the timeout value can be passed to the `wait` method.
116
+
117
+ ```ruby
118
+ scenario "A user can log in successfully" do
119
+ visit new_session_path
120
+
121
+ fill_in "Email", with: "john@example.com"
122
+ fill_in "Password", with: "secret"
123
+ click_button "Log In"
124
+
125
+ wait(3.seconds).for { current_path }.to eq(account_path)
126
+ expect(page).to have_content("Welcome back!")
127
+ end
128
+ ```
129
+
82
130
  ## Who wrote RSpec::Wait?
83
131
 
84
132
  My name is Steve Richert and I wrote RSpec::Wait in April, 2014 with the support
@@ -1,18 +1,49 @@
1
1
  require "rspec"
2
-
3
2
  require "rspec/wait/error"
4
3
  require "rspec/wait/handler"
4
+ require "rspec/wait/proxy"
5
5
  require "rspec/wait/target"
6
6
 
7
7
  module RSpec
8
8
  module Wait
9
+ module_function
10
+
9
11
  # From: https://github.com/rspec/rspec-expectations/blob/v3.0.0/lib/rspec/expectations/syntax.rb#L72-L74
10
12
  def wait_for(value = Target::UndefinedValue, &block)
11
13
  Target.for(value, block)
12
14
  end
15
+
16
+ def wait(timeout = nil, options = {})
17
+ options[:timeout] = timeout
18
+ Proxy.new(options)
19
+ end
20
+
21
+ def with_wait(options)
22
+ original_timeout = RSpec.configuration.wait_timeout
23
+ original_delay = RSpec.configuration.wait_delay
24
+
25
+ RSpec.configuration.wait_timeout = options[:timeout] if options[:timeout]
26
+ RSpec.configuration.wait_delay = options[:delay] if options[:delay]
27
+
28
+ yield
29
+ ensure
30
+ RSpec.configuration.wait_timeout = original_timeout
31
+ RSpec.configuration.wait_delay = original_delay
32
+ end
13
33
  end
14
34
  end
15
35
 
16
36
  RSpec.configure do |config|
17
37
  config.include(RSpec::Wait)
38
+
39
+ config.add_setting(:wait_timeout, default: 10)
40
+ config.add_setting(:wait_delay, default: 0.1)
41
+
42
+ config.around do |example|
43
+ if options = example.metadata[:wait]
44
+ with_wait(options) { example.run }
45
+ else
46
+ example.run
47
+ end
48
+ end
18
49
  end
@@ -1,25 +1,19 @@
1
- require "rspec"
2
1
  require "timeout"
3
2
 
4
- require "rspec/wait/error"
5
-
6
3
  module RSpec
7
4
  module Wait
8
5
  module Handler
9
- TIMEOUT = 10
10
- DELAY = 0.1
11
-
12
6
  def handle_matcher(target, *args, &block)
13
7
  failure = nil
14
8
 
15
- Timeout.timeout(TIMEOUT) do
9
+ Timeout.timeout(RSpec.configuration.wait_timeout) do
16
10
  loop do
17
11
  begin
18
12
  actual = target.respond_to?(:call) ? target.call : target
19
13
  super(actual, *args, &block)
20
14
  break
21
15
  rescue RSpec::Expectations::ExpectationNotMetError => failure
22
- sleep DELAY
16
+ sleep RSpec.configuration.wait_delay
23
17
  retry
24
18
  end
25
19
  end
@@ -0,0 +1,13 @@
1
+ module RSpec
2
+ module Wait
3
+ class Proxy
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def for(value = Target::UndefinedValue, &block)
9
+ Target.for(value, block, @options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,7 +1,3 @@
1
- require "rspec"
2
-
3
- require "rspec/wait/handler"
4
-
5
1
  module RSpec
6
2
  module Wait
7
3
  class Target < RSpec::Expectations::ExpectationTarget
@@ -9,30 +5,41 @@ module RSpec
9
5
  UndefinedValue = Module.new
10
6
 
11
7
  # From: https://github.com/rspec/rspec-expectations/blob/v3.0.0/lib/rspec/expectations/expectation_target.rb#L30-L41
12
- def self.for(value, block)
8
+ def self.for(value, block, options = {})
13
9
  if UndefinedValue.equal?(value)
14
10
  unless block
15
11
  raise ArgumentError, "You must pass either an argument or a block to `wait_for`."
16
12
  end
17
- new(block)
13
+ new(block, options)
18
14
  elsif block
19
15
  raise ArgumentError, "You cannot pass both an argument and a block to `wait_for`."
20
16
  else
21
- new(value)
17
+ new(value, options)
22
18
  end
23
19
  end
24
- #
25
- #
20
+
21
+ # From: https://github.com/rspec/rspec-expectations/blob/v3.0.0/lib/rspec/expectations/expectation_target.rb#L25-L27
22
+ def initialize(target, options)
23
+ @wait_options = options
24
+ super(target)
25
+ end
26
+
26
27
  # From: https://github.com/rspec/rspec-expectations/blob/v3.0.0/lib/rspec/expectations/expectation_target.rb#L53-L54
27
28
  def to(matcher = nil, message = nil, &block)
28
29
  prevent_operator_matchers(:to, matcher) unless matcher
29
- PositiveHandler.handle_matcher(@target, matcher, message, &block)
30
+ with_wait { PositiveHandler.handle_matcher(@target, matcher, message, &block) }
30
31
  end
31
32
 
32
33
  # From: https://github.com/rspec/rspec-expectations/blob/v3.0.0/lib/rspec/expectations/expectation_target.rb#L66-L67
33
34
  def not_to(matcher = nil, message = nil, &block)
34
35
  prevent_operator_matchers(:not_to, matcher) unless matcher
35
- NegativeHandler.handle_matcher(@target, matcher, message, &block)
36
+ with_wait { NegativeHandler.handle_matcher(@target, matcher, message, &block) }
37
+ end
38
+
39
+ private
40
+
41
+ def with_wait
42
+ Wait.with_wait(@wait_options) { yield }
36
43
  end
37
44
  end
38
45
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "rspec-wait"
5
- spec.version = "0.0.3"
5
+ spec.version = "0.0.4"
6
6
 
7
7
  spec.author = "Steve Richert"
8
8
  spec.email = "steve.richert@gmail.com"
@@ -17,5 +17,5 @@ Gem::Specification.new do |spec|
17
17
  spec.add_dependency "rspec", ">= 2.11", "< 3.2"
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.7"
20
- spec.add_development_dependency "rake", "~> 10.3"
20
+ spec.add_development_dependency "rake", "~> 10.4"
21
21
  end
@@ -1,5 +1,3 @@
1
- require "spec_helper"
2
-
3
1
  describe "wait_for" do
4
2
  let!(:progress) { "" }
5
3
 
@@ -43,6 +41,25 @@ describe "wait_for" do
43
41
  }.to raise_error(RSpec::Wait::TimeoutError)
44
42
  end
45
43
 
44
+ it "respects a timeout specified in configuration" do
45
+ original_timeout = RSpec.configuration.wait_timeout
46
+ RSpec.configuration.wait_timeout = 3
47
+
48
+ begin
49
+ expect {
50
+ wait_for { sleep 4; progress }.to eq("..")
51
+ }.to raise_error(RSpec::Wait::TimeoutError)
52
+ ensure
53
+ RSpec.configuration.wait_timeout = original_timeout
54
+ end
55
+ end
56
+
57
+ it "respects a timeout specified in options", wait: { timeout: 3 } do
58
+ expect {
59
+ wait_for { sleep 4; progress }.to eq("..")
60
+ }.to raise_error(RSpec::Wait::TimeoutError)
61
+ end
62
+
46
63
  it "raises an error occuring in the block" do
47
64
  expect {
48
65
  wait_for { raise RuntimeError }.to eq("..")
@@ -93,6 +110,25 @@ describe "wait_for" do
93
110
  }.to raise_error(RSpec::Wait::TimeoutError)
94
111
  end
95
112
 
113
+ it "respects a timeout specified in configuration" do
114
+ original_timeout = RSpec.configuration.wait_timeout
115
+ RSpec.configuration.wait_timeout = 3
116
+
117
+ begin
118
+ expect {
119
+ wait_for { sleep 4; progress }.not_to eq("..")
120
+ }.to raise_error(RSpec::Wait::TimeoutError)
121
+ ensure
122
+ RSpec.configuration.wait_timeout = original_timeout
123
+ end
124
+ end
125
+
126
+ it "respects a timeout specified in options", wait: { timeout: 3 } do
127
+ expect {
128
+ wait_for { sleep 4; progress }.not_to eq("..")
129
+ }.to raise_error(RSpec::Wait::TimeoutError)
130
+ end
131
+
96
132
  it "raises an error occuring in the block" do
97
133
  expect {
98
134
  wait_for { raise RuntimeError }.not_to eq("")
@@ -0,0 +1,170 @@
1
+ describe "wait" do
2
+ let!(:progress) { "" }
3
+
4
+ before do
5
+ Thread.new do
6
+ 2.times do
7
+ sleep 1
8
+ progress << "."
9
+ end
10
+ end
11
+ end
12
+
13
+ context "for" do
14
+ context "to" do
15
+ it "passes immediately" do
16
+ expect {
17
+ wait.for { progress }.to eq("")
18
+ }.not_to raise_error
19
+ end
20
+
21
+ it "waits for the matcher to pass" do
22
+ expect {
23
+ wait.for { progress }.to eq(".")
24
+ }.not_to raise_error
25
+ end
26
+
27
+ it "re-evaluates the actual value" do
28
+ expect {
29
+ wait.for { progress.dup }.to eq(".")
30
+ }.not_to raise_error
31
+ end
32
+
33
+ it "fails if the matcher never passes" do
34
+ expect {
35
+ wait.for { progress }.to eq("...")
36
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
37
+ end
38
+
39
+ it "times out if the block never finishes" do
40
+ expect {
41
+ wait.for { sleep 11; progress }.to eq("..")
42
+ }.to raise_error(RSpec::Wait::TimeoutError)
43
+ end
44
+
45
+ it "respects a timeout specified in configuration" do
46
+ original_timeout = RSpec.configuration.wait_timeout
47
+ RSpec.configuration.wait_timeout = 3
48
+
49
+ begin
50
+ expect {
51
+ wait.for { sleep 4; progress }.to eq("..")
52
+ }.to raise_error(RSpec::Wait::TimeoutError)
53
+ ensure
54
+ RSpec.configuration.wait_timeout = original_timeout
55
+ end
56
+ end
57
+
58
+ it "respects a timeout specified in options", wait: { timeout: 3 } do
59
+ expect {
60
+ wait.for { sleep 4; progress }.to eq("..")
61
+ }.to raise_error(RSpec::Wait::TimeoutError)
62
+ end
63
+
64
+ it "respects a timeout specified as an argument" do
65
+ expect {
66
+ wait(3).for { sleep 4; progress }.to eq("..")
67
+ }.to raise_error(RSpec::Wait::TimeoutError)
68
+ end
69
+
70
+ it "raises an error occuring in the block" do
71
+ expect {
72
+ wait.for { raise RuntimeError }.to eq("..")
73
+ }.to raise_error(RuntimeError)
74
+ end
75
+
76
+ it "prevents operator matchers" do
77
+ expect {
78
+ wait.for { progress }.to == "."
79
+ }.to raise_error(ArgumentError)
80
+ end
81
+
82
+ it "accepts a value rather than a block" do
83
+ expect {
84
+ wait.for(progress).to eq(".")
85
+ }.not_to raise_error
86
+ end
87
+ end
88
+
89
+ context "not_to" do
90
+ it "passes immediately" do
91
+ expect {
92
+ wait.for { progress }.not_to eq("..")
93
+ }.not_to raise_error
94
+ end
95
+
96
+ it "waits for the matcher not to pass" do
97
+ expect {
98
+ wait.for { progress }.not_to eq("")
99
+ }.not_to raise_error
100
+ end
101
+
102
+ it "re-evaluates the actual value" do
103
+ expect {
104
+ wait.for { progress.dup }.not_to eq("")
105
+ }.not_to raise_error
106
+ end
107
+
108
+ it "fails if the matcher always passes" do
109
+ expect {
110
+ wait.for { progress }.not_to be_a(String)
111
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
112
+ end
113
+
114
+ it "times out if the block never finishes" do
115
+ expect {
116
+ wait.for { sleep 11; progress }.not_to eq("..")
117
+ }.to raise_error(RSpec::Wait::TimeoutError)
118
+ end
119
+
120
+ it "respects a timeout specified in configuration" do
121
+ original_timeout = RSpec.configuration.wait_timeout
122
+ RSpec.configuration.wait_timeout = 3
123
+
124
+ begin
125
+ expect {
126
+ wait.for { sleep 4; progress }.not_to eq("..")
127
+ }.to raise_error(RSpec::Wait::TimeoutError)
128
+ ensure
129
+ RSpec.configuration.wait_timeout = original_timeout
130
+ end
131
+ end
132
+
133
+ it "respects a timeout specified in options", wait: { timeout: 3 } do
134
+ expect {
135
+ wait.for { sleep 4; progress }.not_to eq("..")
136
+ }.to raise_error(RSpec::Wait::TimeoutError)
137
+ end
138
+
139
+ it "respects a timeout specified as an argument" do
140
+ expect {
141
+ wait(3).for { sleep 4; progress }.not_to eq("..")
142
+ }.to raise_error(RSpec::Wait::TimeoutError)
143
+ end
144
+
145
+ it "raises an error occuring in the block" do
146
+ expect {
147
+ wait.for { raise RuntimeError }.not_to eq("")
148
+ }.to raise_error(RuntimeError)
149
+ end
150
+
151
+ it "respects the to_not alias" do
152
+ expect {
153
+ wait.for { progress }.to_not eq("..")
154
+ }.not_to raise_error
155
+ end
156
+
157
+ it "prevents operator matchers" do
158
+ expect {
159
+ wait.for { progress }.not_to == ".."
160
+ }.to raise_error(ArgumentError)
161
+ end
162
+
163
+ it "accepts a value rather than a block" do
164
+ expect {
165
+ wait.for(progress).not_to eq("..")
166
+ }.not_to raise_error
167
+ end
168
+ end
169
+ end
170
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-wait
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Richert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-29 00:00:00.000000000 Z
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '10.3'
53
+ version: '10.4'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '10.3'
60
+ version: '10.4'
61
61
  description: Wait for conditions in RSpec
62
62
  email: steve.richert@gmail.com
63
63
  executables: []
@@ -82,10 +82,12 @@ files:
82
82
  - lib/rspec/wait.rb
83
83
  - lib/rspec/wait/error.rb
84
84
  - lib/rspec/wait/handler.rb
85
+ - lib/rspec/wait/proxy.rb
85
86
  - lib/rspec/wait/target.rb
86
87
  - rspec-wait.gemspec
87
88
  - spec/spec_helper.rb
88
89
  - spec/wait_for_spec.rb
90
+ - spec/wait_spec.rb
89
91
  homepage: https://github.com/laserlemon/rspec-wait
90
92
  licenses:
91
93
  - MIT
@@ -106,10 +108,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
108
  version: '0'
107
109
  requirements: []
108
110
  rubyforge_project:
109
- rubygems_version: 2.4.2
111
+ rubygems_version: 2.4.3
110
112
  signing_key:
111
113
  specification_version: 4
112
114
  summary: Wait for conditions in RSpec
113
115
  test_files:
114
116
  - spec/spec_helper.rb
115
117
  - spec/wait_for_spec.rb
118
+ - spec/wait_spec.rb