Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

ruby - Rails Fixtures not loading with rspec

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...