Rails 夹具未使用 rspec 加载
因此,我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将固定装置定义为“用户”,则使用它们的方法是通过同名的方法:
单数仅与类本身(用户)相关。如果这只是一个字母错误,希望您还有一些头发。
If you define fixtures as 'users', then the way to use them is via the method with the same name:
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.
如果您想在全局范围内设置您的装置,您可以在
spec_helper
或rails_helper
内添加:If you want to set up your fixtures globally, inside your
spec_helper
orrails_helper
you can add:我自己花了很长时间才弄清楚这一点。
然后,正如其他评论所述,使用
It took me a really long time to figure this out myself.
then, as stated on other comments, use