28

So, I'm trying to learn the rspec BDD testing framework in the context of a rails project. The problem I'm having is that I can't, for the life of me, get my fixtures to load properly in rspec descriptions.

Disclaimer: Yes, there are better things than fixtures to use. I'm trying to learn one thing at a time, here (specifically rspec) before I go play with associated tools like factory-girl, mocha, auto-test, etc. As such, I'm trying to get the dead-simple, if clunky, fixtures working.

Anyway, here's the code:

/test/fixtures/users.yml -

# password: "secret"
foo:
  username: foo
  email: [email protected]
  password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
  password_salt: bef65e058905c379436d80d1a32e7374b139e7b0

bar:
  username: bar
  email: [email protected]
  password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
  password_salt: bef65e058905c379436d80d1a32e7374b139e7b0

/spec/controllers/pages_controller_spec.rb -

require 'spec/spec_helper'

describe PagesController do
  integrate_views
  fixtures :users
  it "should render index template on index call when logged in" do
    session[:user_id] = user(:foo).id
    get 'index' 
    response.should render_template('index')
  end
end

And what I'm getting when I run 'rake spec' is:

NoMethodError in 'PagesController should render index template on index call when logged in'
undefined method `user' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0x2405a7c>
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/test_process.rb:511:in `method_missing'
./spec/controllers/pages_controller_spec.rb:7:

That is, it's not recognizing 'user(:foo)' as a valid method.

The fixtures themselves must be ok, since when I load them into the development db via 'rake db:fixtures:load', I can verify that foo and bar are present in that db.

I feel like I'm missing something obvious here, but I've been tearing my hair out all day to no avail. Any help would be appreciated.

4
  • How are you using the fixtures from test/fixtures inside Rspec? Would you mind sharing this with us
    – mrateb
    Commented Mar 13, 2019 at 5:45
  • This question is 10 years old. When I first asked it, I was learning rails for the first time (probably rails 2!). Since then I've graduated college, worked at 3 jobs, spoken at rubyconf, and been employed fulltime as a rails dev for ~6 years. I would humbly suggest that perhaps this question is no longer relevant, either to me or to the wider world. :) Commented Mar 14, 2019 at 18:25
  • 1
    hahahhah So as an experienced developer, ure saying that fixtures have a better alternative? :)
    – mrateb
    Commented Mar 15, 2019 at 6:26
  • Not sure why fixtures gets all this hate? Fixtures are good enough for a business like basecamp who've collected over +$100m in revenue over the last 20 years. my two cents - It cannot be all that bad.
    – BenKoshy
    Commented Apr 18, 2023 at 4:54

3 Answers 3

35

If you define fixtures as 'users', then the way to use them is via the method with the same name:

describe PagesController do
  integrate_views
  fixtures :users
  it "should render index template on index call when logged in" do
    session[:user_id] = users(:foo).id
    get 'index'
    response.should render_template('index')
  end
end

The singular is only relevant to the class itself (User). Hope you still have some hair left if this is just a one letter bug.

1
  • 1
    Damnit. Well, I was sure it was something that stupid, and I was was damn right. Thanks, that solved it. :) Commented Dec 29, 2009 at 20:49
18

If you want to set up your fixtures globally, inside your spec_helper or rails_helper you can add:

RSpec.configure do |config|
  config.global_fixtures = :all
end
1
  • It works, but I have to write it in rails_helper.
    – Quv
    Commented Feb 21, 2021 at 5:58
2

It took me a really long time to figure this out myself.

# spec/rails_helper.rb

RSpec.configure do |config|
#  config.file_fixture_path = Rails.root / 'test' / 'fixtures' # <= incorrect
  config.fixture_path = Rails.root / 'test' / 'fixtures'

then, as stated on other comments, use

RSpec.describe 'Events API', type: :request do
  fixtures :events

1
  • Thanks. This helped me fix No such file or directory @ rb_sysopen - /opt/app/spec/fixtures/my_fixture.yml
    – Denn
    Commented Jul 14, 2021 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.