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

set_fixture_class doesn't work in config.before block #510

Closed
skizzybiz opened this issue Feb 28, 2012 · 6 comments
Closed

set_fixture_class doesn't work in config.before block #510

skizzybiz opened this issue Feb 28, 2012 · 6 comments

Comments

@skizzybiz
Copy link

This does not work:

RSpec.configure do |config|
  config.before { set_fixture_class :foo => Bar }
end

If I try to run that, it tells me: spec_helper.rb:6:in 'included': undefined method 'set_fixture_class' for FixtureClasses:Module (NoMethodError).

In order to get the equivalent functionality, I need to jump through hoops like:

module FixtureClasses
  extend ActiveSupport::Concern
  included do
    set_fixture_class :foo => Bar
  end
end

RSpec.configure do |config|
  config.include FixtureClasses
end
@dchelimsky
Copy link
Contributor

That's because set_fixture_class is a class method, but before hooks run in instance scope. You'll need to do something like:

config.before {
  self.class.class_eval {
    set_fixture_class :foo => Bar
  }
}

@justinko
Copy link
Contributor

Can you just do:

RSpec.configure do |config|
  config.set_fixture_class :foo => Bar
end

@skizzybiz
Copy link
Author

@justinko : that gives me undefined method 'set_fixture_class' for #<RSpec::Core::Configuration:0x007fb813d3fe78> (NoMethodError)

@justinko
Copy link
Contributor

@skizzybiz sorry @dchelimsky is right, it is a class method.

@moses
Copy link

moses commented Apr 11, 2012

Since set_fixture_class is a public method, you can also do this:

config.before do
  self.class.set_fixture_class :foo => Bar
end

@weilu
Copy link

weilu commented Jul 12, 2012

config.before does not quite work when you also have before blocks that reference fixture foo. In those cases the before block closer to the spec gets invoked first, which would again throw FixtureClassNotFound: No class attached to find. Using before(:all) solves it for me.

config.before(:all) do
  self.class.set_fixture_class :foo => Bar
end

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

No branches or pull requests

5 participants