【发布时间】:2021-06-09 21:42:45
【问题描述】:
我正在尝试在我的代码中编写一个类来包装一些 RSpec 调用。但是,每当我尝试访问 rspec 事物时,我的班级根本看不到方法。
我在spec/support/helper.rb中定义了以下文件
require 'rspec/mocks/standalone'
module A
class Helper
def wrap_expect(dbl, func, args, ret)
expect(dbl).to receive(func).with(args).and_return(ret)
end
end
end
我得到了NoMethodError: undefined method 'expect',尽管需要正确的模块。请注意,如果我在模块之前调用 rspec 函数,一切都会正确找到。
我已尝试将以下赞添加到我的spec_helper.rb:
config.requires << 'rspec/mocks/standalone'
但无济于事。
我设法在我的类中使用类变量并从全局上下文中传递函数,但这种解决方案似乎相当极端。我也能够传递测试上下文本身并存储它,但我也不想这样做。
【问题讨论】:
-
将此行
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }添加到spec_helper.rb -
我确实有那行,模块在我的测试中被正确导入,只是在 Helper 类中找不到 rspec 函数。
标签: ruby-on-rails ruby rspec