Skip to content

Commit

Permalink
Use an OpenStruct for config
Browse files Browse the repository at this point in the history
... instead of plain structs. This more closely matches how
Railtie::Configuration works, and avoids having to define a new struct
with different fields in every test.
  • Loading branch information
richardTowers committed Jul 23, 2024
1 parent d6a2c4a commit 0f00140
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions spec/lib/govuk_timezone_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
require "ostruct"
require "spec_helper"
require "govuk_app_config/govuk_timezone"

RSpec.describe GovukError do
describe ".configure" do
let(:config) { OpenStruct.new }
let(:logger) { instance_double("ActiveSupport::Logger") }

before do
allow(Rails).to receive(:logger).and_return(logger)
end

it "should override the default UTC time_zone to London" do
config = Struct.new(:time_zone).new("UTC")
config.time_zone = "UTC"
expect(logger).to receive(:info)
GovukTimezone.configure(config)
expect(config.time_zone).to eq("London")
end

it "should allow apps to set time_zone explicitly with config.govuk_time_zone" do
config = Struct.new(:time_zone, :govuk_time_zone).new("UTC", "Shanghai")
config.time_zone = "UTC"
config.govuk_time_zone = "Shanghai"
GovukTimezone.configure(config)
expect(config.time_zone).to eq("Shanghai")
end

it "should default to London if config.govuk_time_zone is nil" do
config = Struct.new(:time_zone, :govuk_time_zone).new("UTC", nil)
config.time_zone = "UTC"
config.govuk_time_zone = nil
expect(logger).to receive(:info)
GovukTimezone.configure(config)
expect(config.time_zone).to eq("London")
end

it "should raise an error if config.time_zone is set to anything other than the default UTC" do
config = Struct.new(:time_zone).new("London")
config.time_zone = "London"
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/)
config = Struct.new(:time_zone).new("Shanghai")
config.time_zone = "Shanghai"
expect { GovukTimezone.configure(config) }.to raise_error(/govuk_app_config prevents configuring time_zone with config[.]time_zone/)
end
end
Expand Down

0 comments on commit 0f00140

Please sign in to comment.