因此,我尝试在 Rails 项目的上下文中学习 rspec BDD 测试框架。我遇到的问题是,在我的一生中,我无法在 rspec 描述中正确加载我的固定装置。
免责声明:是的,有比固定装置更好的东西可以使用。在我开始使用相关工具(如 factory-girl、mocha、auto-test 等)之前,我试图一次学习一件事,在这里(特别是 rspec)。因此,我试图让死的简单,如果笨重,固定装置工作。
无论如何,这是代码:
/test/fixtures/users.yml -
# password: "secret"
foo:
username: foo
email: foo@example.com
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
bar:
username: bar
email: bar@example.com
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 是否存在于该数据库中。
我觉得我在这里遗漏了一些明显的东西,但我整天都在撕扯我的头发却无济于事。任何帮助将不胜感激。
最佳答案
如果您将 fixtures 定义为“用户”,那么使用它们的方式是通过同名方法:
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
单数仅与类本身(用户)相关。如果这只是一个字母错误,希望你还有一些头发。
关于ruby-on-rails - Rails Fixtures 没有加载 rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1976832/