Rake不会吞下RSpec消息输出

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

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 

– 暂时禁用Ruby中的警告| 良好的代码

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

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

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

此外,您可以使用随后的钩子在RSpec描述块中围绕所有测试包装silent_warnings ; 例如

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

再次,谨慎使用它