ruby-on-rails - 在之前使用 "file_fixture"(:suite)?

标签 ruby-on-rails testing rspec rspec-rails

我想在 spec_helper 中执行此操作:

config.before(:suite) do
  allow(Net::HTTP)
    .to receive(:get)
    .with(URI(some_uri))
    .and_return(file_fixture('response.json').read)
end

但不幸的是,这不起作用:

NoMethodError:
  undefined method `file_fixture' for #<RSpec::Core::AnonymousExampleGroup (no description provided)>

但是当我这样做的时候:

config.before(:each) do
...
end

有用吗?

为什么会这样,是否也可以在 before(:suite) block 中使用 file_fixture

最佳答案

file_fixture 显然仅在示例级别可用。这意味着您可以在 itspecifylet block 中使用它。

它的作用 - 它只是一个读取文件的简单助手,您可以这样修复它:

config.before(:suite) do
  allow(Net::HTTP)
    .to receive(:get)
    .with(URI(some_uri))
    .and_return(File.read('path/to/your/fixtures/response.json'))
end

关于ruby-on-rails - 在之前使用 "file_fixture"(:suite)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58739582/

相关文章:

ruby - 在 Ruby (rspec) 中测试私有(private)方法

ruby-on-rails - 运行捆绑安装时出现错误

ruby-on-rails - 字符串的未定义方法 `serializable_hash'

ruby-on-rails - Rails 应用程序中的代理 Websocket 请求

linux - 使用熔岩运行测试用例

python - 产生大量 REST API 流量的方法

testing - 如何使用 Jest 在 Next 中测试 _document

javascript - 使用 capybara 单击 div 规范外部

ruby-on-rails - 弃用警告:ActionController::TestCase HTTP 请求方法在未来的 Rails 版本中将只接受关键字参数

ruby-on-rails - 索引无事件记录的Elasticsearch