Skip to content

Streamline RSpec config #2264

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 4 commits into from
Jan 20, 2020
Merged

Streamline RSpec config #2264

merged 4 commits into from
Jan 20, 2020

Conversation

pirj
Copy link
Member

@pirj pirj commented Jan 12, 2020

Disable monkey patching and fix specs related to this.

@pirj pirj self-assigned this Jan 12, 2020
@pirj pirj force-pushed the update-spec_helper-to-use-defaults branch 2 times, most recently from ed5824f to 43bb489 Compare January 14, 2020 18:30
Copy link
Member Author

@pirj pirj left a comment

Choose a reason for hiding this comment

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

@JonRowe please take another look. Enabled partial double verification.

@@ -225,6 +235,7 @@ def controller

it 'is accessible to hooks' do
with_isolated_config do
view = double("view")
Copy link
Member Author

Choose a reason for hiding this comment

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

No idea why it was passing before.

Copy link
Member

Choose a reason for hiding this comment

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

Is view not also a method?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't seem so, at least not it the bare context of ExampleGroup.describe.
This is needed here.
Picked it up here.
I suspect that with_isolated_config was swallowing NoMethodError previously.

Copy link
Member

Choose a reason for hiding this comment

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

Nope nor these links

Copy link
Member

Choose a reason for hiding this comment

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

with_isolated_config does not swallow errors, it just runs an ensure

Copy link
Member

@JonRowe JonRowe Jan 15, 2020

Choose a reason for hiding this comment

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

It's defined here: lib/rspec/rails/example/view_example_group.rb:81 so this should be removed...

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 see your point.

When I comment out with_isolated_config and locally defined view double, the hook is not executed. It is not executed with with_isolated_config or if metadata condition is removed.

The following doesn't work either:

      context 'with a hook' do
        let(:run_count) { [] }

        before(:each, :type => :view) do
          allow(view).to receive(:a_stubbed_helper) { :value }
          run_count << 1
        end

        it 'is accessible to hooks' do
          group = RSpec::Core::ExampleGroup.describe 'a view', :type => :view do
            specify { true }
          end
          group.run NullObject.new
          expect(run_count.first).to eq 1
        end
      end

Any suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

You need to keep with_isolated_config, if your changes here are causing an error you need to ensure the right methods are defined, doing double here is just wrong..

Copy link
Member Author

Choose a reason for hiding this comment

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

A call to allow(view).to receive(:a_stubbed_helper) { :value } results in:

RSpec::Mocks::MockExpectationError: #<#<Class:0x00007fa59165da18>:0x00007fa59165ceb0 @_config={}, ...> does not implement: a_stubbed_helper

that for some reason doesn't bubble up.
The same happens if you move stubbing directly to the example:

          group = RSpec::Core::ExampleGroup.describe 'a view', :type => :view do
            specify {
              allow(view).to receive(:a_stubbed_helper) { :value }
              run_count += 1
            }
          end

Example returns prematurely, run_count is not incremented, but there's no exception from mocks. Is it a bug?

Changed to stub an existing render method instead.

Removed NullObject.new from group.run's arguments, it doesn't seem to affect this in any way.

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

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

Looking good, a few questions mostly out of curiosity.

@@ -225,6 +235,7 @@ def controller

it 'is accessible to hooks' do
with_isolated_config do
view = double("view")
Copy link
Member

Choose a reason for hiding this comment

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

Is view not also a method?

@@ -225,6 +235,7 @@ def controller

it 'is accessible to hooks' do
with_isolated_config do
view = double("view")
Copy link
Member

@JonRowe JonRowe Jan 15, 2020

Choose a reason for hiding this comment

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

It's defined here: lib/rspec/rails/example/view_example_group.rb:81 so this should be removed...

@@ -225,6 +235,7 @@ def controller

it 'is accessible to hooks' do
with_isolated_config do
view = double("view")
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 see your point.

When I comment out with_isolated_config and locally defined view double, the hook is not executed. It is not executed with with_isolated_config or if metadata condition is removed.

The following doesn't work either:

      context 'with a hook' do
        let(:run_count) { [] }

        before(:each, :type => :view) do
          allow(view).to receive(:a_stubbed_helper) { :value }
          run_count << 1
        end

        it 'is accessible to hooks' do
          group = RSpec::Core::ExampleGroup.describe 'a view', :type => :view do
            specify { true }
          end
          group.run NullObject.new
          expect(run_count.first).to eq 1
        end
      end

Any suggestions?

@pirj pirj force-pushed the update-spec_helper-to-use-defaults branch from 87b77ab to 47893b8 Compare January 16, 2020 09:07
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

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

Apart from figuring out the double issue this looks almost good to go, I think theres a bad rebase gone on though...

@pirj pirj force-pushed the update-spec_helper-to-use-defaults branch from 07afbbc to ba3db74 Compare January 19, 2020 09:33
@pirj pirj requested a review from JonRowe January 19, 2020 10:23
@pirj
Copy link
Member Author

pirj commented Jan 19, 2020

Rebase fixed, all green.

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

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

Looking really good but I spotted one last thing!

run_count += 1
end
group = RSpec::Core::ExampleGroup.describe 'a view', :type => :view do
specify { true }
end
group.run NullObject.new
group.run
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this cause the groups output to be output to the real formatter?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no difference in the output.

Copy link
Member

Choose a reason for hiding this comment

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

I was surprised by that assertion, as the point of this object is to prevent the real reporter being used, but its failing locally for me? I want to investigate this further as its not possible to prove either way on CI without changing the format...

What I expect this to do is to add the spec you've specified above into the reporter, so if you run this spec on its own you'd see "2 examples passed" for this one example.

Copy link
Member Author

Choose a reason for hiding this comment

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

If I run it focused (rspec spec/rspec/rails/example/view_example_group_spec.rb:236), it fails locally. However, the spec when run altogether passes.
This weird behavior remains even with just one example left, and if you remove module namespace, and with_isolated_, and nesting contexts. I've put it on my list to investigate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe it doesn't add to the main reporter because of https://github.com/rspec/rspec-rails/pull/2264/files#diff-93830fa29d616f7c87903d08b5b1b29aR55 ?

  config.around(:example) do |example|
    real_world = RSpec.world
    RSpec.instance_variable_set(:@world, RSpec::Core::World.new)
    example.run
    RSpec.instance_variable_set(:@world, real_world)
  end

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'm not sure if with_isolated_config is even necessary in specs with that hook.

Copy link
Member

Choose a reason for hiding this comment

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

So I got to the bottom of why this doesn't run when focused. If you're interested its an artefact in our filtering, with_isolated_config does not remove filters from this spec, as they exist in the world and the world is not isolated. That is something I might consider fixing at some point but is waaaaaaaaay out of scope for this.

I also confirmed this removal is ok, we now use a NullReporter by default, a change which post-dates this spec I suspect.

Copy link
Member

@benoittgt benoittgt left a comment

Choose a reason for hiding this comment

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

Thanks!

run_count += 1
end
group = RSpec::Core::ExampleGroup.describe 'a view', :type => :view do
specify { true }
end
group.run NullObject.new
group.run
Copy link
Member

Choose a reason for hiding this comment

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

So I got to the bottom of why this doesn't run when focused. If you're interested its an artefact in our filtering, with_isolated_config does not remove filters from this spec, as they exist in the world and the world is not isolated. That is something I might consider fixing at some point but is waaaaaaaaay out of scope for this.

I also confirmed this removal is ok, we now use a NullReporter by default, a change which post-dates this spec I suspect.

@JonRowe JonRowe merged commit e23efbb into master Jan 20, 2020
@JonRowe JonRowe deleted the update-spec_helper-to-use-defaults branch January 20, 2020 21:04
JonRowe added a commit that referenced this pull request Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants