Ruby-on-rails Rails Fixtures 未使用 rspec 加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1976832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:05:26  来源:igfitidea点击:

Rails Fixtures not loading with rspec

ruby-on-railsrubytestingrspecfixtures

提问by Fishtoaster

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.

所以,我正在尝试在 Rails 项目的上下文中学习 rspec BDD 测试框架。我遇到的问题是,在我的一生中,我无法在 rspec 描述中正确加载我的装置。

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.

免责声明:是的,有比固定装置更好的东西可以使用。我正在尝试一次学习一件事,在这里(特别是 rspec),然后我开始使用相关工具,如工厂女孩、摩卡咖啡、自动测试等。因此,我试图变得非常简单,如果笨重,固定装置工作。

Anyway, here's the code:

无论如何,这是代码:

/test/fixtures/users.yml -

/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 -

/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:

当我运行“rake spec”时,我得到的是:

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.

也就是说,它没有将 'user(:foo)' 识别为有效方法。

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.

夹具本身必须没问题,因为当我通过“rake db:fixtures:load”将它们加载到开发数据库时,我可以验证该数据库中是否存在 foo 和 bar。

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.

我觉得我在这里遗漏了一些明显的东西,但我整天都在扯头发却无济于事。任何帮助,将不胜感激。

回答by tadman

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.

单数只与类本身(用户)相关。如果这只是一个字母错误,希望你还有一些头发。

回答by lobati

If you want to set up your fixtures globally, inside your spec_helperor rails_helperyou can add:

如果你想全局设置你的灯具,在你的spec_helper或者rails_helper你可以添加:

RSpec.configure do |config|
  config.global_fixtures = :all
end