我正在按照本教程使用 Rails 5.0 创建一个小 API http://apionrails.icalialabs.com/book/frontmatter
我正在尝试运行位于 app/config/lib
的测试但 RSpec 找不到测试。
我尝试添加 config.autoload_paths << Rails.root.join('lib')
或者
config.autoload_paths += %W(#{config.root}/lib)
至app/config/application.rb
.
这是我的代码:
$ /app/config/application.rb
#required files go here, deleted for clarity.
Bundler.require(*Rails.groups)
module MarketPlaceApi
class Application < Rails::Application
# don't generate RSpec tests for views and helpers
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.helper_specs false
g.stylesheets = false
g.javascripts = false
g.helper = false
end
config.autoload_paths += %W(#{config.root}/lib)
end
end
测试
$ app/config/lib/spec/test_spec.rb
require 'spec_helper'
describe ApiConstraints do
#test goes here
end
如何添加 lib
文件夹,以便 RSpec 运行位于其中的测试?
提前致谢。
最佳答案
RSpec 不会在 autoload_paths
中查找测试。如果您运行不带参数的 rspec
或 rake spec
,RSpec 默认会在 spec
目录中查找。如果您想在不同的目录中运行规范,请将该目录作为参数传递给 rspec:
rspec app/config/lib/spec
但是,通用约定是将规范保存在 spec
目录中,与 app
和 lib
中的代码分开,所以我会将您的规范移至 spec/lib
。然后,RSpec 将无需任何额外努力即可找到它们。
关于ruby-on-rails - 如何配置 RSpec 以在 lib 文件夹中查找规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38870796/