Skip to content

Fix helper specs with internal classes #1499

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

Merged
merged 2 commits into from
Dec 10, 2015
Merged
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
11 changes: 9 additions & 2 deletions lib/rspec/rails/example/helper_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ module HelperExampleGroup

# @private
module ClassMethods
def determine_default_helper_class(_ignore)
described_class
if ::Rails::VERSION::MAJOR > 3
def determine_constant_from_test_name(_ignore)
described_class if yield(described_class)
end
else
def determine_default_helper_class(_ignore)
return unless Module === described_class && !(Class === described_class)
Copy link
Member Author

Choose a reason for hiding this comment

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

I want to rewrite this boolean expression to read with less complexity.

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not groking why theres two different names for these methods

Copy link
Member Author

Choose a reason for hiding this comment

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

these have to be rails hooks

described_class
end
end
end

Expand Down
31 changes: 25 additions & 6 deletions spec/rspec/rails/example/helper_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

module RSpec::Rails
describe HelperExampleGroup do
module ::FoosHelper; end
module ::FoosHelper
class InternalClass
end
end

subject { HelperExampleGroup }

it_behaves_like "an rspec-rails example group mixin", :helper,
Expand Down Expand Up @@ -48,11 +52,26 @@ def _view

describe HelperExampleGroup::ClassMethods do
describe "determine_default_helper_class" do
it "returns the helper module passed to describe" do
helper_spec = Object.new.extend HelperExampleGroup::ClassMethods
allow(helper_spec).to receive(:described_class) { FoosHelper }
expect(helper_spec.determine_default_helper_class("ignore this")).
to eq(FoosHelper)
let(:group) do
RSpec::Core::ExampleGroup.describe do
include HelperExampleGroup
end
end

context "the described is a module" do
it "returns the module" do
allow(group).to receive(:described_class) { FoosHelper }
expect(group.determine_default_helper_class("ignore this")).
to eq(FoosHelper)
end
end

context "the described is a class" do
it "returns nil" do
allow(group).to receive(:described_class) { FoosHelper::InternalClass }
expect(group.determine_default_helper_class("ignore this")).
to be_nil
end
end
end
end
Expand Down