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

Warn on context fixture helper #1442

Closed
cupakromer opened this issue Aug 2, 2015 · 2 comments · Fixed by #1501
Closed

Warn on context fixture helper #1442

cupakromer opened this issue Aug 2, 2015 · 2 comments · Fixed by #1501
Assignees

Comments

@cupakromer
Copy link
Member

Fixtures are controlled by the underlying ActiveRecord::TestFixtures code. Which sets up fixtures before each example. While the database may have fixture data in it before this point, that data will be reset once the test fixture code runs.

When the fixtures message is sent in an example group, or global fixtures are defined, the Active Record test fixtures register the accessors in an anonymous module. That module is included in the example group's ancestors.

Which means the accessor helpers are available in any example group instance; thus they are available in before(:context) and after(:context). However, the internal fixture setup and cache has not been performed yet. Attempting to call a fixture helper prior to the fixtures being setup, raises an undefined method on NilClass error.

Related: #1201 and #1208

@fables-tales
Copy link
Member

Reproduction case is put a file called spec/models/widgets_spec.rb in a rails app with a "Widget" model.

require 'rails_helper'

RSpec.describe Widget, type: :model do
  fixtures :widgets

  describe 'fixture loaded in a before context' do
    before :context do
      widgets :a
    end

    it 'works' do
    end
  end
end

@fables-tales
Copy link
Member

@cupakromer I have a sketch of a fix for this. I'm going to clean it up a little and PR it tomorrow.

@fables-tales fables-tales self-assigned this Dec 6, 2015
fables-tales pushed a commit that referenced this issue Dec 8, 2015
Fixes #1442.

The basic approach here is to capture the addition of the fixture
methods to the example group instance and then monkeypatch them. The
monkeypatch checks to see if we're currently in a before(:context) hook
and if we are then it prints a warning and doesn't invoke the method.

The warning here is a little sparse at the moment, and I'd like to make
it more clear. One thing that's a little gross about this implementation
is that it uses the inspect string of the example group to determine if
we're in a before(:context) hook. As far as I can tell there isn't a
better way to make that determination, but maybe someone's got a clever
trick.
fables-tales pushed a commit that referenced this issue Dec 10, 2015
Fixes #1442.

The basic approach here is to capture the addition of the fixture
methods to the example group instance and then monkeypatch them. The
monkeypatch checks to see if we're currently in a before(:context) hook
and if we are then it prints a warning and doesn't invoke the method.

The warning here is a little sparse at the moment, and I'd like to make
it more clear. One thing that's a little gross about this implementation
is that it uses the inspect string of the example group to determine if
we're in a before(:context) hook. As far as I can tell there isn't a
better way to make that determination, but maybe someone's got a clever
trick.
fables-tales pushed a commit that referenced this issue Dec 14, 2015
Fixes #1442.

The basic approach here is to capture the addition of the fixture
methods to the example group instance and then monkeypatch them. The
monkeypatch checks to see if we're currently in a before(:context) hook
and if we are then it prints a warning and doesn't invoke the method.

The warning here is a little sparse at the moment, and I'd like to make
it more clear. One thing that's a little gross about this implementation
is that it uses the inspect string of the example group to determine if
we're in a before(:context) hook. As far as I can tell there isn't a
better way to make that determination, but maybe someone's got a clever
trick.
fables-tales pushed a commit that referenced this issue Dec 15, 2015
Fixes #1442.

The basic approach here is to capture the addition of the fixture
methods to the example group instance and then monkeypatch them. The
monkeypatch checks to see if we're currently in a before(:context) hook
and if we are then it prints a warning and doesn't invoke the method.

The warning here is a little sparse at the moment, and I'd like to make
it more clear. One thing that's a little gross about this implementation
is that it uses the inspect string of the example group to determine if
we're in a before(:context) hook. As far as I can tell there isn't a
better way to make that determination, but maybe someone's got a clever
trick.
fables-tales pushed a commit that referenced this issue Dec 15, 2015
sebjacobs pushed a commit to futurelearn/rspec-rails that referenced this issue Mar 15, 2019
Fixes rspec#1442.

The basic approach here is to capture the addition of the fixture
methods to the example group instance and then monkeypatch them. The
monkeypatch checks to see if we're currently in a before(:context) hook
and if we are then it prints a warning and doesn't invoke the method.

The warning here is a little sparse at the moment, and I'd like to make
it more clear. One thing that's a little gross about this implementation
is that it uses the inspect string of the example group to determine if
we're in a before(:context) hook. As far as I can tell there isn't a
better way to make that determination, but maybe someone's got a clever
trick.
sebjacobs pushed a commit to futurelearn/rspec-rails that referenced this issue Mar 15, 2019
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 a pull request may close this issue.

2 participants