在Rails应用程序中,require 'capybara/rails'是否在spec/rails_helper.rb中运行?还有require 'capybara/rspec'怎么样?它在spec/rspec_helper.rb中吗?而不是规范/rails_helper.rb.这两个文件的目的是什么?
发布于 2014-07-14 01:11:55
您可以将其添加到rails_helper.rb文件的顶部。下面是他们的文档的设置说明。下面是我的rails_helper顶部和放置require 'capybara/rspec'的位置的一个例子。
该文件的目的是加载使用特定配置运行的每个测试。因此,将行require 'rails_helper'添加到每个测试文件的顶部,如下所示:
require 'rails_helper'
RSpec.describe MyModel, :type => :model do
# add your tests here
end我发现这个资源对于其中两个文件的新使用很有帮助。rails_helper.rb文件是RSpec 3x中的新文件,而在RSpec 2x中,默认的辅助文件是spec_helper.rb。
下面是我的rails_helper.rb文件中前几行的示例。它应该会告诉您将代码require 'capybara/rspec'放在哪里。
ENV['RAILS_ENV'] = 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rspec'作为每个测试运行的特定配置的一个例子,当我测试图像上传时,我想在每次测试之后删除上传的文件,所以我配置了我的rails_helper.rb。
config.after(:each) do
if Rails.env.test? || Rails.env.cucumber?
FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
end
end对于一些应用程序,我也喜欢使用斗牛士,要使用这个特性,您必须确保测试需要它,这样就可以将require 'shoulda/matchers添加到rails_helper.rb中。
https://stackoverflow.com/questions/24728321
复制相似问题