ruby 如何在aruba rspec中测试长时间运行的命令?

wmvff8tz  于 2023-05-22  发布在  Ruby
关注(0)|答案(1)|浏览(98)

我有一个使用aruba的RSpec文件./spec/pkg/abc_spec.rb,其中包含一个测试,当命令花费很长时间时,该测试通常会崩溃,但如果命令快速返回,则不会崩溃。
堆栈跟踪失败;

# <GEM_PATH>/aruba-2.1.0/lib/aruba/rspec.rb:35:in `block (3 levels) in <top (required)>'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/platforms/local_environment.rb:22:in `call'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/platforms/unix_platform.rb:79:in `with_environment'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/api/core.rb:222:in `block in with_environment'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/platforms/unix_environment_variables.rb:189:in `nest'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/api/core.rb:220:in `with_environment'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/rspec.rb:34:in `block (2 levels) in <top (required)>'
# <GEM_PATH>/aruba-2.1.0/lib/aruba/rspec.rb:25:in `block (2 levels) in <top (required)>'

有问题的规范./spec/pkg/abc_spec.rb看起来像这样(实际的命令已经被替换为一个sleep,它需要的时间和它一样长);

require "aruba/rspec"

RSpec.describe "long running test", :type => :aruba do
  context "long command" do
    before { run_command('bash -c "sleep 20 && echo yikes"') }
    it { expect(last_command_started).to have_output "yikes" }
  end
end

在测试一个需要很长时间的命令时,有没有什么方法可以防止它出错?

dgiusagp

dgiusagp1#

Aruba测试可以配置为允许长时间运行命令。run_command将遵循exit_timeout配置,可以在spec文件中设置,例如;

Aruba.configure do |config|
  # some time longer than your run_command should take
  config.exit_timeout = 21
end

对于所提供的块,使用此Aruba.configure的完整示例是:

require "aruba/rspec"

Aruba.configure do |config|
  config.exit_timeout = 21
end

RSpec.describe "long running test", :type => :aruba do
  context "long command" do
    before { run_command('bash -c "sleep 20 && echo yikes"') }
    it { expect(last_command_started).to have_output "yikes" }
  end
end

相关问题