Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 5317c30

Browse files
committed
Merge pull request #1288 from rspec/remove-sleeping-in-specs
Remove sleeps from formatter specs
2 parents 519cd43 + 4c67387 commit 5317c30

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

lib/rspec/core/example.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def description
7171
# running this example.
7272
attr_reader :example_group_instance
7373

74+
# @attr_accessor
75+
# @private
76+
attr_accessor :clock
77+
7478
# Creates a new instance of Example.
7579
# @param example_group_class the subclass of ExampleGroup in which this Example is declared
7680
# @param description the String passed to the `it` method (or alias)
@@ -81,6 +85,7 @@ def initialize(example_group_class, description, metadata, example_block=nil)
8185
@metadata = @example_group_class.metadata.for_example(description, metadata)
8286
@example_group_instance = @exception = nil
8387
@pending_declared_in_example = false
88+
@clock = RSpec::Core::Time
8489
end
8590

8691
# @deprecated access options via metadata instead
@@ -248,7 +253,7 @@ def with_around_each_hooks(&block)
248253

249254
def start(reporter)
250255
reporter.example_started(self)
251-
record :started_at => RSpec::Core::Time.now
256+
record :started_at => clock.now
252257
end
253258

254259
def finish(reporter)
@@ -272,7 +277,7 @@ def finish(reporter)
272277
end
273278

274279
def record_finished(status, results={})
275-
finished_at = RSpec::Core::Time.now
280+
finished_at = clock.now
276281
record results.merge(:status => status, :finished_at => finished_at, :run_time => (finished_at - execution_result[:started_at]).to_f)
277282
end
278283

spec/rspec/core/formatters/base_text_formatter_spec.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ def run_all_and_dump_pending
260260

261261
before do
262262
group = RSpec::Core::ExampleGroup.describe("group") do
263-
# Use a sleep so there is some measurable time, to ensure
264-
# the reported percent is 100%, not 0%.
265-
example("example") { sleep 0.001 }
266-
example_line_number = __LINE__ - 1
263+
example("example") do |example|
264+
# make it look slow without actually taking up precious time
265+
example.clock = class_double(RSpec::Core::Time, :now => RSpec::Core::Time.now + 0.5)
266+
end
267+
example_line_number = __LINE__ - 4
267268
end
268269
group.run(reporter)
269270

@@ -297,9 +298,10 @@ def run_all_and_dump_pending
297298
describe "#dump_profile_slowest_example_groups", :slow do
298299
let(:group) do
299300
RSpec::Core::ExampleGroup.describe("slow group") do
300-
# Use a sleep so there is some measurable time, to ensure
301-
# the reported percent is 100%, not 0%.
302-
example("example") { sleep 0.01 }
301+
example("example") do |example|
302+
# make it look slow without actually taking up precious time
303+
example.clock = class_double(RSpec::Core::Time, :now => RSpec::Core::Time.now + 0.5)
304+
end
303305
end
304306
end
305307

@@ -320,8 +322,8 @@ def run_all_and_dump_pending
320322
context "with multiple example groups" do
321323
before do
322324
group2 = RSpec::Core::ExampleGroup.describe("fast group") do
323-
example("example 1") { sleep 0.004 }
324-
example("example 2") { sleep 0.007 }
325+
example("example 1") { }
326+
example("example 2") { }
325327
end
326328
group2.run(reporter)
327329

spec/rspec/core/formatters/json_formatter_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@
113113

114114
before do
115115
group = RSpec::Core::ExampleGroup.describe("group") do
116-
# Use a sleep so there is some measurable time, to ensure
117-
# the reported percent is 100%, not 0%.
118-
example("example") { sleep 0.001 }
116+
example("example") { }
119117
end
120118
group.run(reporter)
121119

@@ -142,9 +140,10 @@
142140
describe "#dump_profile_slowest_example_groups", :slow do
143141
let(:group) do
144142
RSpec::Core::ExampleGroup.describe("slow group") do
145-
# Use a sleep so there is some measurable time, to ensure
146-
# the reported percent is 100%, not 0%.
147-
example("example") { sleep 0.01 }
143+
example("example") do |example|
144+
# make it look slow without actually taking up precious time
145+
example.clock = class_double(RSpec::Core::Time, :now => RSpec::Core::Time.now + 0.5)
146+
end
148147
end
149148
end
150149

@@ -165,8 +164,8 @@
165164
context "with multiple example groups", :slow do
166165
before do
167166
group2 = RSpec::Core::ExampleGroup.describe("fast group") do
168-
example("example 1") { sleep 0.004 }
169-
example("example 2") { sleep 0.007 }
167+
example("example 1") { }
168+
example("example 2") { }
170169
end
171170
group2.run(reporter)
172171

0 commit comments

Comments
 (0)