Ruby on rails 未加载rspec的轨道固定装置
因此,我试图在rails项目的上下文中学习rspec BDD测试框架。我面临的问题是,我无法在rspec描述中正确加载我的固定装置 免责声明:是的,有比固定装置更好的东西可以使用。我试图一次学习一件事,在这里(特别是rspec),然后再使用相关的工具,如factory girl、mocha、auto test等。因此,我试图让非常简单(如果笨重)的固定装置工作 不管怎样,代码如下: /测试/夹具/用户.yml-Ruby on rails 未加载rspec的轨道固定装置,ruby-on-rails,ruby,testing,rspec,fixtures,Ruby On Rails,Ruby,Testing,Rspec,Fixtures,因此,我试图在rails项目的上下文中学习rspec BDD测试框架。我面临的问题是,我无法在rspec描述中正确加载我的固定装置 免责声明:是的,有比固定装置更好的东西可以使用。我试图一次学习一件事,在这里(特别是rspec),然后再使用相关的工具,如factory girl、mocha、auto test等。因此,我试图让非常简单(如果笨重)的固定装置工作 不管怎样,代码如下: /测试/夹具/用户.yml- # password: "secret" foo: username: foo
# 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
/规格/控制器/页面\u控制器\u规格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:
“登录时,PagesController应在索引调用时呈现索引模板”中的NoMethodError
未定义的方法“user”#
/Library/Ruby/Gems/1.8/Gems/actionpack-2.3.5/lib/action\u controller/test\u process.rb:511:in'method\u missing'
./spec/controller/pages\u controller\u spec.rb:7:
也就是说,它没有将“user(:foo)”识别为有效的方法
装置本身必须是正常的,因为当我通过“rakedb:fixtures:load”将它们加载到开发数据库中时,我可以验证该数据库中是否存在foo和bar
我觉得我错过了一些明显的东西,但我整天都在胡思乱想,结果一无所获。任何帮助都将不胜感激。如果您将装置定义为“用户”,则使用它们的方法是使用相同名称的方法:
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
单数仅与类本身(用户)相关。如果这只是一个单字母错误,希望您还有一些头发。如果您想全局设置设备,可以在
spec\u帮助器
或rails\u帮助器
中添加:
RSpec.configure do |config|
config.global_fixtures = :all
end
我自己花了很长时间才弄明白
# 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内部测试/夹具中的夹具?你介意和我们分享一下吗?这个问题已经有10年了。当我第一次问它时,我是第一次学习rails(可能是Rails2!)。从那时起,我已经大学毕业,做过3份工作,在rubyconf工作过,并全职担任rails开发人员约6年。我谦恭地说,也许这个问题对我或整个世界都不再重要了哈哈哈,作为一名经验丰富的开发人员,你是说固定装置有更好的选择吗?:)它可以工作,但我必须在
rails\u helper
中编写它。
RSpec.describe 'Events API', type: :request do
fixtures :events