Rails 夹具未使用 rspec 加载

发布于 2024-08-17 01:25:04 字数 1806 浏览 4 评论 0原文

因此,我试图在 Rails 项目的背景下学习 rspec BDD 测试框架。我遇到的问题是,我一生都无法让我的装置在 rspec 描述中正确加载。

免责声明:是的,有比固定装置更好的东西可以使用。在我使用 Factory-girl、mocha、自动测试等相关工具之前,我试图一次学习一件事(特别是 rspec)。因此,我试图变得非常简单,如果笨重,固定装置可以工作。

无论如何,这里是代码:

/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

当我运行“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:

也就是说,它无法识别“user(:foo” )' 作为有效方法。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必在意 2024-08-24 01:25:04

如果将固定装置定义为“用户”,则使用它们的方法是通过同名的方法:

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

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

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.

深海不蓝 2024-08-24 01:25:04

如果您想在全局范围内设置您的装置,您可以在 spec_helperrails_helper 内添加:

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

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
梦醒时光 2024-08-24 01:25:04

我自己花了很长时间才弄清楚这一点。

# spec/rails_helper.rb

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

然后,正如其他评论所述,使用

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文