Skip to content

Commit

Permalink
Fix: AppicationController spec
Browse files Browse the repository at this point in the history
Due to some issues between the rpsec-rails gem and Rails 7, possibly
this:

rspec/rspec-rails#2773

this spec stops working in Rails 7.

To work around this, we move in closer and test the behaviour of the
before action itself asserting that the attribute is correct and using
it via the User is also correct.

We no longer need the `reset_all` calls around every spec as we believe
these are being called for us in later version of rspec-rails:

rspec/rspec-rails#2752

We no longer need the DummyController as it was likely not actually
under test.

We take confidence in the other coverage we have that everything is
working as expected for this feature.
  • Loading branch information
mec committed Dec 18, 2024
1 parent ea03d76 commit f28467d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
33 changes: 11 additions & 22 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@ class DummyController < ApplicationController; end
end
end

describe "before_action" do
controller DummyController do
def custom_action
head 200
end
end

before(:each) do
routes.draw do
get "custom_action" => "dummy#custom_action"
end
end

describe "Current attributes before action" do
context "user is not signed in" do
before { allow(controller).to receive(:current_user).and_return(nil) }

it "does not set Current.user_organisation" do
get "custom_action"
controller.send(:set_organisation_list_and_current_organisation)

expect(Current.user_organisation).to be(nil)
end
Expand All @@ -54,23 +44,22 @@ def custom_action
context "user is signed in" do
let(:user) { create(:partner_organisation_user) }

before do
allow(controller).to receive(:current_user).and_return(user)
end
before { allow(controller).to receive(:current_user).and_return(user) }

it "does not set Current.user_organisation if `current_user_organisation` is not in the session" do
get "custom_action"
controller.send(:set_organisation_list_and_current_organisation)

expect(Current.user_organisation).to be(nil)
expect(controller.current_user.current_organisation_id).to eql(user.primary_organisation.id)
end

it "sets Current.user_organisation if `current_user_organisation` is in the session" do
id = SecureRandom.uuid
session[:current_user_organisation] = id
session[:current_user_organisation] = "a-fake-id"

get "custom_action"
controller.send(:set_organisation_list_and_current_organisation)

expect(Current.user_organisation).to be(id)
expect(Current.user_organisation).to eql("a-fake-id")
expect(controller.current_user.current_organisation_id).to eql("a-fake-id")
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
ActiveSupport::CurrentAttributes.reset_all
end

config.after(:each) do |example|
ActionMailer::Base.deliveries.clear
ActiveSupport::CurrentAttributes.reset_all
end

config.before(:each, type: :request) do
Expand Down

0 comments on commit f28467d

Please sign in to comment.