RSpec 不加载我的 class,即使它在 Rails 控制台中加载

RSpec doesn't load my class even though it loads in the Rails console

我在 app/models/parser 中有一个名为 data.rb 的 class,内容为:

class Parser::Data 
  def method1
  end
end

目前没有什么特别的。我正在尝试在实施太多之前为其编写测试,只是为 Rails.

安装了默认 RSpec

我的 RSpec 文件在 spec/models/parser/data_spec.rb 中并且到目前为止非常基础:

require 'spec_helper.rb'

describe Parser::Data do
  let(:parser) { Parser::Data.new }
end

当我 运行 测试时我得到这个错误:

spec/models/parser/data_spec.rb:3:in `<top (required)>': uninitialized constant Parser (NameError)

我尝试将 module Parser 放置在同一目录 Data class 周围 app/models/parser,我还尝试将其移动到 lib/parser 执行相同的模块包装 class,并在 application.rb 中添加了 lib/parser 以自动加载,但到目前为止没有任何效果。

我做错了什么?

require 'rails_helper' 而不是 spec_helper。仅要求 spec_helper 为我重现了该问题,要求 rails_helper 修复了该问题。有关 spec_helperrails_helper(包括性能影响)的更多信息,请参见此处:How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

我用 运行ning RSpec 和 bundle exec rspec 重现了这个问题。如果我 运行 RSpec 和 bin/rspec (这是由 spring-commands-rspec gem 生成的 binstub)它不关心哪个我需要的帮助文件。我想 spring 加载得更急切。