Skip to content

Commit 47d6f8a

Browse files
Remove load_defaults in favor of better helper
Previous specs were full of `load_defaults` and `reload_routes` at random places that were probably meant to reset state left around by other specs. I removed all of those in favor of a better behaved helper that cleans up after itself, and explicit loading of the resources needed by each spec. Doing this surfaced several other state leakages which I fixed.
1 parent 2313967 commit 47d6f8a

12 files changed

+90
-65
lines changed

spec/requests/default_namespace_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def with_custom_default_namespace(namespace)
7979
def with_temp_application(application)
8080
original_application = ActiveAdmin.application
8181
ActiveAdmin.application = application
82-
load_defaults!
83-
reload_routes!
82+
83+
load_resources { ActiveAdmin.register(Category) }
8484

8585
yield
8686

spec/requests/memory_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'rails_helper'
22

33
RSpec.describe "Memory Leak", type: :request, if: RUBY_ENGINE == 'ruby' do
4-
before do
5-
load_defaults!
4+
around do |example|
5+
with_resources_during(example) { ActiveAdmin.register(Category) }
66
end
77

88
def count_instances_of(klass)
@@ -15,7 +15,7 @@ def count_instances_of(klass)
1515
GC.start
1616
count = count_instances_of(klass)
1717

18-
load_defaults!
18+
load_resources { ActiveAdmin.register(Category) }
1919

2020
GC.start
2121
GC.disable if previously_disabled

spec/support/active_admin_integration_spec_helper.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module ActiveAdminIntegrationSpecHelper
2-
def load_defaults!
3-
load_resources do
4-
ActiveAdmin.register(Category)
5-
ActiveAdmin.register(User)
6-
ActiveAdmin.register(Post){ belongs_to :user, optional: true }
7-
end
2+
def with_resources_during(example)
3+
load_resources { yield }
4+
5+
example.run
6+
7+
load_resources {}
88
end
99

1010
def reload_menus!

spec/unit/namespace_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
Admin.send(:remove_const, 'PostsController')
4141
end
4242

43+
after do
44+
load_resources {}
45+
end
46+
4347
it "should not crash" do
4448
expect { ActiveAdmin.unload! }.not_to raise_error
4549
end

spec/unit/page_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module ActiveAdmin
55
RSpec.describe Page do
66

77
it_should_behave_like "ActiveAdmin::Resource"
8-
before { load_defaults! }
98

109
let(:application){ ActiveAdmin::Application.new }
1110
let(:namespace){ Namespace.new(application, :admin) }

spec/unit/resource/naming_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
module ActiveAdmin
44
RSpec.describe Resource, "Naming" do
5-
6-
before { load_defaults! }
7-
85
let(:application){ ActiveAdmin::Application.new }
96
let(:namespace){ Namespace.new(application, :admin) }
107

spec/unit/resource/pagination_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
module ActiveAdmin
44
RSpec.describe Resource, "Pagination" do
5-
6-
before { load_defaults! }
7-
85
let(:application){ ActiveAdmin::Application.new }
96
let(:namespace){ Namespace.new(application, :admin) }
107

spec/unit/resource/routes_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
require 'rails_helper'
22

33
RSpec.describe ActiveAdmin::Resource::Routes do
4-
5-
after do
6-
load_defaults!
7-
reload_routes!
8-
end
9-
104
let(:application) { ActiveAdmin.application }
115
let(:namespace) { application.namespace(:admin) }
126

137
context "when in the admin namespace" do
148
let(:config) { namespace.resource_for('Category') }
159

16-
before do
17-
load_resources { ActiveAdmin.register Category }
10+
around do |example|
11+
with_resources_during(example) { ActiveAdmin.register Category }
1812
end
1913

2014
let(:category) { Category.new { |c| c.id = 123 } }
@@ -35,6 +29,10 @@
3529
context "when in the root namespace" do
3630
let!(:config) { ActiveAdmin.register Category, namespace: false }
3731

32+
around do |example|
33+
with_resources_during(example) { config }
34+
end
35+
3836
after do
3937
application.namespace(:root).unload!
4038
application.namespaces.instance_variable_get(:@namespaces).delete(:root)
@@ -45,15 +43,17 @@
4543
end
4644

4745
it "should generate a correct route" do
48-
reload_routes!
4946
expect(config.route_collection_path).to eq "/categories"
5047
end
5148
end
5249

5350
context "when registering a plural resource" do
5451
class ::News; def self.has_many(*); end end
5552
let!(:config) { ActiveAdmin.register News }
56-
before{ reload_routes! }
53+
54+
around do |example|
55+
with_resources_during(example) { config }
56+
end
5757

5858
it "should return the plural route with _index" do
5959
expect(config.route_collection_path).to eq "/admin/news"

spec/unit/resource/scopes_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
module ActiveAdmin
44
RSpec.describe Resource, "Scopes" do
5-
6-
before { load_defaults! }
7-
85
let(:application){ ActiveAdmin::Application.new }
96
let(:namespace){ Namespace.new(application, :admin) }
107

spec/unit/resource_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module ActiveAdmin
55
RSpec.describe Resource do
66

77
it_should_behave_like "ActiveAdmin::Resource"
8-
before { load_defaults! }
8+
9+
around do |example|
10+
with_resources_during(example) { namespace.register Category }
11+
end
912

1013
let(:application){ ActiveAdmin::Application.new }
1114
let(:namespace){ Namespace.new(application, :admin) }
@@ -38,6 +41,10 @@ def config(options = {})
3841
expect(config.decorator_class).to eq nil
3942
end
4043
context 'when a decorator is defined' do
44+
around do |example|
45+
with_resources_during(example) { resource }
46+
end
47+
4148
let(:resource) { namespace.register(Post) { decorate_with PostDecorator } }
4249
specify '#decorator_class_name should return PostDecorator' do
4350
expect(resource.decorator_class_name).to eq '::PostDecorator'
@@ -64,6 +71,10 @@ def config(options = {})
6471
describe "#include_in_menu?" do
6572
subject{ resource }
6673

74+
around do |example|
75+
with_resources_during(example) { resource }
76+
end
77+
6778
context "when regular resource" do
6879
let(:resource){ namespace.register(Post) }
6980
it { is_expected.to be_include_in_menu }
@@ -239,6 +250,10 @@ class MockResource
239250
describe '#find_resource' do
240251
let(:post) { double }
241252

253+
around do |example|
254+
with_resources_during(example) { resource }
255+
end
256+
242257
context 'without a decorator' do
243258
let(:resource) { namespace.register(Post) }
244259

@@ -284,6 +299,10 @@ class MockResource
284299
end
285300
end
286301

302+
after do
303+
Admin.send(:remove_const, :"PostsController")
304+
end
305+
287306
it 'can find the post by controller finder' do
288307
allow(Post).to receive(:find_by_title!).with('title-name').and_return(post)
289308

0 commit comments

Comments
 (0)