Rspec - 为什么我必须在我的规范文件中要求 active_support

Rspec - why did I have to require active_support in my spec file

我刚开始使用 rspec 编写一些单元测试。

在我的规格文件中,我必须添加 require 'active_support/core_ext' 行,因为我 运行 测试的其中一种方法使用 blank? 方法。没有它,我在未知方法 blank.

的行中遇到错误

规格如下:

it 'returns error if receives a string as payload' do
    expect {ensure_properties([:title], 'ABC')}.to raise_error(MyError)
end

并且 ensure_properties 像这样使用空白:

if value.is_a? String and value.blank?
   raise_my_error('Something')
end

我只是想知道为什么规范文件本身需要有那个要求。规范不只是 运行 从那个函数得到结果吗?

Isn't the spec just running to get the result from that function?

或多或少。但是 function/method 需要 blank?。您可以将其放入测试方法的文件中,而不是将其放入您的规范文件中,这样它就可以工作了。

您提到了主动支持。这是 rails 应用程序吗?如果是这样,正确的做法是在您的 rails_helper/spec_helper 中加载 rails 环境。 (除非你不是故意这样做的,为了加快规范或类似的事情)。