Rake 不会吞下 RSpec 消息输出

Rake does not swallow RSpec message output

我的规范提供了我所希望的覆盖范围,但是,以下 2 条消息显示在 rspec 输出中:

rake resque:scheduler
rake environment resque:work

我如何在规范运行期间吞下这些东西,以免它们搞砸我的 nyancat 格式化程序?

规格

describe 'database rake task' do
  include_context 'rake'
  let(:task_paths) { ['tasks/heroku'] }

  before do
    invoke_task.reenable
  end

  # rubocop:disable all
  describe 'myapp:heroku' do
    context ':setup' do
      context ':secrets' do
        let(:task_name) { 'myapp:heroku:setup:secrets' }

        context 'with env' do
          it 'works' do
            expect(File).to receive(:exist?).with('.env').and_return(true)
            expect_any_instance_of(Object).to receive(:system).with('heroku config:push --remote production').and_return(true)
            expect { invoke_task.invoke }.to output(
              "\nUpdating Secrets for production\n"
            ).to_stdout
          end
        end

        context 'without env' do
          it 'works' do
            expect(File).to receive(:exist?).with('.env').and_return(false)
            expect { invoke_task.invoke }.to raise_error("\nYou are missing the .env file\n").and(output(
              "\nUpdating Secrets for production\n"
            ).to_stdout)
          end
        end
      end
    end
  end

  describe 'schedule_and_work' do
    let(:task_name) { 'schedule_and_work' }

    context 'with process fork' do
      it 'works' do
        expect(Process).to receive(:fork).and_return(true)
        expect_any_instance_of(Object).to receive(:system).with('rake environment resque:work', {}).and_return(true)
        expect(invoke_task.invoke).to be
      end
    end

    context 'without process fork' do
      it 'works' do
        expect(Process).to receive(:fork).and_return(false)
        expect(Process).to receive(:wait).and_return(true)
        expect_any_instance_of(Object).to receive(:system).with('rake resque:scheduler', {}).and_return(true)
        expect(invoke_task.invoke).to be
      end
    end
  end
  # rubocop:enable all
end

耙任务

namespace :myapp do
  namespace :heroku do
    namespace :setup do
      desc 'modify secrets'
      task :secrets do
        puts "\nUpdating Secrets for production\n"
        raise "\nYou are missing the .env file\n" unless File.exist?('.env')
        system('heroku config:push --remote production')
      end
    end
  end
end

# Run resque scheduler on 2 free dynos
# https://grosser.it/2012/04/14/resque-scheduler-on-heroku-without-extra-workers/
task :schedule_and_work do
  if Process.fork
    sh 'rake environment resque:work'
  else
    sh 'rake resque:scheduler'
    Process.wait
  end
end

您可以使用以下测试辅助方法

require 'stringio'

def silent_warnings
  old_stderr = $stderr
  $stderr = StringIO.new
  yield
ensure
  $stderr = old_stderr
end

-- Temporarily disabling warnings in Ruby | Virtuous Code

并用silent_warnings方法包装Rake任务的调用;像这样

silent_warnings do
  expect { invoke_task.invoke }.to output(
    "\nUpdating Secrets for production\n"
  ).to_stdout
end

但是,请谨慎使用它,因为它吞没了块代码中产生的所有警告(打印到 $stdout),使将来更难调试。

此外,您可以使用 around hooksilent_warnings 包裹在 RSpec describe 块中的所有测试;例如

around(:example) do |example|
  silent_warnings { example.run }
end

同样,请谨慎使用