Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run before(:all) hooks for rails magic example groups #829

Closed
wants to merge 7 commits into from
Closed
1 change: 1 addition & 0 deletions lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -146,6 +146,7 @@ def method_missing(method, *args, &block)
subject { controller }

metadata[:type] = :controller
hooks.register_globals(self, RSpec.configuration.hooks)

before do
self.routes = ::Rails.application.routes
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/feature_example_group.rb
Expand Up @@ -7,6 +7,7 @@ module FeatureExampleGroup

included do
metadata[:type] = :feature
hooks.register_globals(self, RSpec.configuration.hooks)

app = ::Rails.application
if app.respond_to?(:routes)
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/helper_example_group.rb
Expand Up @@ -31,6 +31,7 @@ def _controller_path(example)

included do
metadata[:type] = :helper
hooks.register_globals(self, RSpec.configuration.hooks)

before do |example|
controller.controller_path = _controller_path(example)
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/rails/example/mailer_example_group.rb
Expand Up @@ -7,6 +7,8 @@ module MailerExampleGroup

included do
metadata[:type] = :mailer
hooks.register_globals(self, RSpec.configuration.hooks)

include ::Rails.application.routes.url_helpers
options = ::Rails.configuration.action_mailer.default_url_options
options.each { |key, value| default_url_options[key] = value } if options
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/model_example_group.rb
Expand Up @@ -5,6 +5,7 @@ module ModelExampleGroup

included do
metadata[:type] = :model
hooks.register_globals(self, RSpec.configuration.hooks)
end
end
end
6 changes: 6 additions & 0 deletions lib/rspec/rails/example/rails_example_group.rb
Expand Up @@ -10,6 +10,12 @@ module RailsExampleGroup
include RSpec::Rails::MinitestLifecycleAdapter if ::Rails::VERSION::STRING >= '4'
include RSpec::Rails::MinitestAssertionAdapter
include RSpec::Rails::Matchers

def set_metadata_type(type)
metadata[:type] = type
hooks.register_globals(self, RSpec.configuration.hooks)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this isn't used anywhere? Looks like a useful abstraction, though; if we stick with this solution for some reason it would be good to leverage this. (Although I suspect the final fix will be in rspec-core).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yeah, I tried to DRY it up (couldn't get it to work cause of the included scoping)


end
end
end
1 change: 1 addition & 0 deletions lib/rspec/rails/example/request_example_group.rb
Expand Up @@ -14,6 +14,7 @@ def app

included do
metadata[:type] = :request
hooks.register_globals(self, RSpec.configuration.hooks)

before do
@routes = ::Rails.application.routes
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/routing_example_group.rb
Expand Up @@ -31,6 +31,7 @@ def routes(&blk)

included do
metadata[:type] = :routing
hooks.register_globals(self, RSpec.configuration.hooks)

before do
self.routes = ::Rails.application.routes
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/rails/example/view_example_group.rb
Expand Up @@ -143,6 +143,8 @@ def _include_controller_helpers
include ExampleMethods

metadata[:type] = :view
hooks.register_globals(self, RSpec.configuration.hooks)

helper(*_default_helpers)

before do
Expand Down
4 changes: 3 additions & 1 deletion spec/rspec/rails/example/controller_example_group_spec.rb
@@ -1,6 +1,6 @@
require "spec_helper"

class ::ApplicationController
class ::ApplicationController < ActionController::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I run the group of specs to check that they work and the hooks are invoked. Controller specs require an actual controller to work. (And the build's still failing)

end

module RSpec::Rails
Expand Down Expand Up @@ -96,5 +96,7 @@ module RSpec::Rails
controller_class.superclass.should eq(ApplicationController)
end
end

it_behaves_like "runs metadata hooks of :type =>", :controller, ControllerExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/feature_example_group_spec.rb
Expand Up @@ -52,5 +52,7 @@ def visit(url)
expect(group.new.visit("/foo")).to eq("success: /foo")
end
end

it_behaves_like "runs metadata hooks of :type =>", :feature, FeatureExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/helper_example_group_spec.rb
Expand Up @@ -51,6 +51,8 @@ def _view
group.new.helper.should be_kind_of(ApplicationHelper)
end
end

it_behaves_like "runs metadata hooks of :type =>", :helper, HelperExampleGroup
end

describe HelperExampleGroup::ClassMethods do
Expand Down
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/mailer_example_group_spec.rb
Expand Up @@ -17,5 +17,7 @@ module ::Rails; end
end
group.metadata[:type].should eq(:mailer)
end

it_behaves_like "runs metadata hooks of :type =>", :mailer, MailerExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/model_example_group_spec.rb
Expand Up @@ -11,5 +11,7 @@ module RSpec::Rails
end
group.metadata[:type].should eq(:model)
end

it_behaves_like "runs metadata hooks of :type =>", :model, ModelExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/request_example_group_spec.rb
Expand Up @@ -13,5 +13,7 @@ module RSpec::Rails
end
group.metadata[:type].should eq(:request)
end

it_behaves_like "runs metadata hooks of :type =>", :request, RequestExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/routing_example_group_spec.rb
Expand Up @@ -28,5 +28,7 @@ module RSpec::Rails
example.foo_path.should == "foo"
end
end

it_behaves_like "runs metadata hooks of :type =>", :routing, RoutingExampleGroup
end
end
2 changes: 2 additions & 0 deletions spec/rspec/rails/example/view_example_group_spec.rb
Expand Up @@ -216,5 +216,7 @@ def _view; end
view_spec.template
end
end

it_behaves_like "runs metadata hooks of :type =>", :view, ViewExampleGroup
end
end
6 changes: 6 additions & 0 deletions spec/support/helpers.rb
Expand Up @@ -16,5 +16,11 @@ def metadata_with(additional_metadata)
m
end

def with_isolated_config
config = RSpec.configuration
yield config.dup if block_given?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dup only produces a shallow duplicate, which means that any mutation on referenced objects will not be isolated by this. To get a deep dup for full isolation you can use Marshal.load(Marshal.dump config)

Also, it's not clear to me how this even works: you are yielding a dup of config, but it's not what RSpec.configuration references, so it seems like within the block, mutations to the yielded config argument won't affect anything that tries to use RSpec.configuration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, good point, I need to set the config. I'm trying not to pollute the global config with my before hooks.

RSpec.configuration = config
end

RSpec.configure {|c| c.include self}
end
22 changes: 22 additions & 0 deletions spec/support/hook_shared_examples.rb
@@ -0,0 +1,22 @@
shared_examples_for "runs metadata hooks of :type =>" do |type, type_group|

[:before, :after].each do |hook|
[:all, :each].each do |scope|

it "runs #{hook} #{scope} hooks before groups of #{type}" do
with_isolated_config do |config|
run_count = 0
config.send(hook, scope, :type => type) { run_count += 1 }
group = RSpec::Core::ExampleGroup.describe do
include type_group
specify { true }
end
group.run RSpec::Core::Reporter.new
expect(run_count).to eq 1
end
end

end
end

end