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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rspec/rails/example/mailer_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module MailerExampleGroup

included do
metadata[:type] = :mailer

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/view_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _include_controller_helpers
include ExampleMethods

metadata[:type] = :view

helper(*_default_helpers)

before do
Expand Down
4 changes: 3 additions & 1 deletion spec/rspec/rails/example/controller_example_group_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module RSpecRails
class Application < ::Rails::Application
self.config.secret_key_base = 'ASecretString' if config.respond_to? :secret_key_base
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,19 @@ def metadata_with(additional_metadata)
m
end

def with_isolated_config
original_config = RSpec.configuration
RSpec.configuration = RSpec::Core::Configuration.new
RSpec.configure do |c|
c.include RSpec::Rails::FixtureSupport
c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
c.add_setting :use_instantiated_fixtures
c.add_setting :global_fixtures
c.add_setting :fixture_path
end
yield
RSpec.configuration = original_config
end

RSpec.configure {|c| c.include self}
end
22 changes: 22 additions & 0 deletions spec/support/hook_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -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
run_count = 0
RSpec.configuration.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