Skip to content

Commit

Permalink
Make random_example_spec.rb tests time-safe
Browse files Browse the repository at this point in the history
I noticed that the test would occasionally fail because the time
would be different between the first run and second run of
`GovukSchemas::RandomExample.new(schema: schema).payload`

The `public_updated_at` might be 16:06:03 for one and 16:06:04 for
the other. This is just a natural consequence of the time it takes
to execute the code, and is not an indicator of the randomness not
being deterministic, which is what the test is testing.
  • Loading branch information
ChrisBAshton committed Jul 22, 2020
1 parent e491224 commit 241b921
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions govuk_schemas.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.4"
spec.add_development_dependency "rubocop-govuk", "~> 3.8"
spec.add_development_dependency "timecop", "~> 0.9"
spec.add_development_dependency "yard", "~> 0.8"

spec.required_ruby_version = ">= 2.6"
Expand Down
15 changes: 9 additions & 6 deletions spec/lib/random_example_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "timecop"
require "spec_helper"

RSpec.describe GovukSchemas::RandomExample do
Expand Down Expand Up @@ -27,12 +28,14 @@

it "returns the same output if a seed is detected" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")
srand(777) # these srand calls would be in the upstream application
first_payload = GovukSchemas::RandomExample.new(schema: schema).payload
srand(777)
second_payload = GovukSchemas::RandomExample.new(schema: schema).payload

expect(first_payload).to eql(second_payload)
# freeze time to avoid inconsistent `public_updated_at` values between runs
Timecop.freeze do
srand(777) # these srand calls would be in the upstream application
first_payload = GovukSchemas::RandomExample.new(schema: schema).payload
srand(777)
second_payload = GovukSchemas::RandomExample.new(schema: schema).payload
expect(first_payload).to eql(second_payload)
end
end

it "can customise the payload" do
Expand Down

0 comments on commit 241b921

Please sign in to comment.