不带滑轨的RSpec无法在规范/支持中加载文件 [英] RSpec without rails not loading files in spec/support
问题描述
在文件spec/support/factory_girl.rb中,我拥有
In the file spec/support/factory_girl.rb I have
fail "At least this file is being required"
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
我有一些规范代码
require 'pmc_article'
require 'pmc_article_parser'
require 'factory_girl'
require_relative './factories/pmc_article_factory'
RSpec.describe PMCArticle do
let(:pmc_article) do
build(:pmc_article)
end
it 'parses pmid' do
expect(pmc_article.article_id_pmid).to eq '123456'
end
end
但是当我运行bundle exec rspec
时我会得到
but when I run bundle exec rspec
I get
1) PMCArticle parses pmid
Failure/Error: build(:pmc_article)
NoMethodError:
undefined method `build' for #<RSpec::ExampleGroups::PMCArticle:0x007feac3cc5078>
# ./spec/pmc_article_spec.rb:8:in `block (2 levels) in <top (required)>'
# ./spec/pmc_article_spec.rb:12:in `block (2 levels) in <top (required)>'
即使工厂,我仍认为rspec不需要spec/support/factory_girl.rb
女孩入门指南说我应该把文件放在那里.
I assume that rspec is failing to require spec/support/factory_girl.rb
, even though the factory girl getting started guide says I should put the file there.
在我开始使用项目的工厂女孩之前,我已经运行了命令行命令来初始化rspec.
I had run the command line command to initialize rspec when I started the project, which was before I was using factory girl for the project.
为什么RSpec无法在规范/支持中加载文件?
Why isn't RSpec loading files in spec/support?
推荐答案
规范/支持没有什么神奇之处.如果您使用rspec-rails,则它会添加一条自动在其中加载所有内容的行,但从3.1.0版本开始,它不会添加行-请参阅此
There is nothing magic about spec/support. If you use rspec-rails then it used to add a line loading everything in there automatically but as of of 3.1.0 it doesn't - see this commit).
我只能假设工厂女孩文档假定此已停用的功能存在.您可以通过添加
I can only assume that the factory girl docs assume the presence of this now deactivated feature. You can turn this on for your project by adding
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
对于规范助手,或者您当然可以将config.include
行移动到主要spec_helper文件中.
To your spec helper or of course you could move that config.include
line into your main spec_helper file.
在非Rails世界中,将Rails.root
替换为项目根目录所在的任何位置.
In a non rails world, replace Rails.root
with wherever the root of your project is.
这篇关于不带滑轨的RSpec无法在规范/支持中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!