2017-03-12 4 views
0

Meine spec Abdeckung bietet, wie ich gehofft hatte, aber die folgenden zwei Meldungen im rspec Ausgabe angezeigten:Rake schluckt nicht RSpec Nachrichtenausgabe

rake resque:scheduler 
rake environment resque:work 

Wie kann ich diese während spec schlucken läuft, so dass sie tun vermasselst du meinen nyancat formatter nicht?

Spec

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 

Rake Aufgabe

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 

enter image description here

Antwort

0

Sie können mit dem folgenden Testhilfsmethode

require 'stringio' 

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

- Temporarily disabling warnings in Ruby | Virtuous Code

Und die rufenden eines Rake Aufgabe mit silent_warnings Verfahren wickeln; wie so

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

Jedoch verwenden sie sparsam, da er alle Warnungen innerhalb des Block-Code erzeugt schlucken (to $stdout gedruckt), so dass es schwieriger in der Zukunft zu debuggen.

Sie können auch silent_warnings um alle Tests innerhalb eines RSpec-desec-Blocks wickeln, indem Sie dann around hook; z.B.

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

Wieder verwenden Sie es sparsam

+0

Awesome! Ich versuche das jetzt. Empfiehlst du das in einen rspec shared helper zu legen? –

+1

Wahrscheinlich Ihr 'rails_helper.rb' Testskript, wenn es noch ziemlich klein ist. Ansonsten als neues Skript innerhalb Ihres 'spec/support'-Verzeichnisses (falls Sie eines haben), da es über die 'rails_helper.rb'-Skriptzeile' Dir [Rails.root.join ("spec/support/*") eingebunden wird. */*. rb ")]. jeder {| Datei | require file} ', falls es existiert. Oder auch nur im Rahmen von Paartests, wo es verwendet wird, weil ich woanders nicht gebraucht werde. – sonna

+0

Das hat perfekt funktioniert, danke. Ja 100% sparsam. Ich würde nur empfehlen, dies zu verwenden, während Rake-Spezifikationen getestet werden, wo zutreffend. Hättest du zufällig Feedback zu http://stackoverflow.com/questions/42753946/ruby-rails-rspec-rake-resque-task-spies? –

Verwandte Themen