Skip to content

Commit

Permalink
Verify that doubled constants exist
Browse files Browse the repository at this point in the history
If a doubled constant doesn't exist then Rails won't verify methods on
it, so we generally want to verify that they do exist. However, we
_don't_ want to verify them in two scenarios:

  - when running isolated spec files (since those constants won't be
    loaded, so would result in verification failures)
  - when TDDing a file (isolated or not) which uses collaborators which
    don't exist yet

In both of those scenarios we're running a single file, so we can use
that as a proxy.

Note that if we were to add it to rails_helper then it would only be
applied to files which were loaded after the first file which loaded
rails helper, which would lead to inconsistent behaviour because of our
random loading.
  • Loading branch information
dgmstuart committed Sep 1, 2024
1 parent 2c98538 commit aba53c4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions spec/models/info_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
content: %w[about_bristol bristol_history]
}
}
pages = instance_double("InfoPages", page: config)
pages = instance_double("InfoPage::InfoPages", page: config)

page = described_class.new(:about, pages:, city: :london)

Expand All @@ -40,7 +40,7 @@
sidebar: %w[lindy_hop about_us_bristol]
}
}
pages = instance_double("InfoPages", page: config)
pages = instance_double("InfoPage::InfoPages", page: config)

page = described_class.new(:about, pages:, city: :bristol)

Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/associated_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
describe "#link" do
it "links to the event" do
event = instance_double("Event")
url_helpers = instance_double("Rails.application.routes.url_helpers")
url_helpers = double("Rails.application.routes.url_helpers") # rubocop:disable RSpec/VerifiedDoubles
allow(url_helpers).to receive(:event_path).with(event).and_return("/path/to/event")

associated_event = described_class.new(event, summarizer: double, url_helpers:)
Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/maps/social_listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
RSpec.describe Maps::SocialListing do
describe ".has_class?" do
it "delegates to the given event" do
has_class = instance_double("Boolean")
has_class = double("Boolean") # rubocop:disable RSpec/VerifiedDoubles
event = instance_double("Event", has_class?: has_class)

social_listing = described_class.new(event, url_helpers: double)
Expand All @@ -20,7 +20,7 @@

describe ".has_taster?" do
it "delegates to the given event" do
has_taster = instance_double("Boolean")
has_taster = double("Boolean") # rubocop:disable RSpec/VerifiedDoubles
event = instance_double("Event", has_taster?: has_taster)

social_listing = described_class.new(event, url_helpers: double)
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/social_listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

describe ".new?" do
it "delegates to the given event" do
new = instance_double("Boolean")
new = double("Boolean") # rubocop:disable RSpec/VerifiedDoubles
event = instance_double("Event", new?: new)

social_listing = described_class.new(event, url_helpers: double)
Expand Down
10 changes: 5 additions & 5 deletions spec/services/session_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe "#create" do
context "when the user has a role in the system" do
it "logs the user in" do
authoriser = instance_double("described_class::Authoriser", authorised?: true)
authoriser = instance_double("SessionCreator::Authoriser", authorised?: true)
login_session = instance_double("LoginSession", log_in!: double)
creator = described_class.new(authoriser:, login_session:, logger: fake_logger)

Expand All @@ -25,7 +25,7 @@
end

it "returns true" do
authoriser = instance_double("described_class::Authoriser", authorised?: true)
authoriser = instance_double("SessionCreator::Authoriser", authorised?: true)
login_session = instance_double("LoginSession", log_in!: double)
creator = described_class.new(authoriser:, login_session:, logger: fake_logger)

Expand All @@ -38,7 +38,7 @@

context "when the user doesn't have a role in the system" do
it "does not log the user in" do
authoriser = instance_double("described_class::Authoriser", authorised?: false)
authoriser = instance_double("SessionCreator::Authoriser", authorised?: false)
login_session = instance_double("LoginSession", log_in!: double)
creator = described_class.new(authoriser:, login_session:, logger: fake_logger)

Expand All @@ -49,7 +49,7 @@
end

it "returns false" do
authoriser = instance_double("described_class::Authoriser", authorised?: false)
authoriser = instance_double("SessionCreator::Authoriser", authorised?: false)
login_session = instance_double("LoginSession", log_in!: double)
creator = described_class.new(authoriser:, login_session:, logger: fake_logger)

Expand All @@ -60,7 +60,7 @@
end

it "logs" do
authoriser = instance_double("described_class::Authoriser", authorised?: false)
authoriser = instance_double("SessionCreator::Authoriser", authorised?: false)
login_session = instance_double("LoginSession", log_in!: double)
logger = fake_logger
creator = described_class.new(authoriser:, login_session:, logger:)
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
if config.files_to_run.count > 1
# Don't verify that doubled objects exist if only runnig one file
mocks.verify_doubled_constant_names = true
end
end

# used for --seed when randomising specs
Expand Down

0 comments on commit aba53c4

Please sign in to comment.