【发布时间】:2021-01-20 20:32:18
【问题描述】:
我想为典型的 Ruby on Rails 项目正确使用 spec/support/ rspec 目录中的 module 文件。
# spec/support/page_objects/foo/bar.rb
module Foo
module Bar
def hello
"hello world"
end
end
end
# spec/support/page_objects/foo/foo_bar.rb
class FooBar
include Foo::Bar
end
# spec/system/foo_bars/create_spec.rb
RSpec.describe "Create FooBar", type: :system do
it "returns hello world"
expect(FooBar.new.hello).to eq "hello world"
end
end
我希望 rspec spec/foo_bars/create_spec.rb 通过,但我收到了 uninitialized constant Foo::Bar。
我可以让它工作的唯一方法是在类文件中要求模块。
# spec/support/page_objects/foo/foo_bar.rb
require_relative("bar")
class FooBar
include Foo::Bar
end
有没有办法正确使用spec/support 中的module 文件来避免必须显式调用require?
【问题讨论】:
-
rails_helper.rb 中有一个注释掉的部分(至少由 gem 生成),它遍历 spec/support 目录及其子目录并需要所有文件。它在 RSpec.configure 部分之上。
标签: ruby-on-rails ruby rspec