0

In my class, I have a method. In the else block, it puts out a message "A number you typed isn't even on the card..." and a copy of the card.

def is_input_valid?(input)
   if current_card.include?(input.first) && current_card.include?(input.last) 
      true
   else 
      puts "A number you typed isn't even on the card..."
      p current_card
      false
   end
end 

In my test, I purposely make the method go to the else block and it passes.

it "returns false if input contains ANY numbers NOT from the current_card" do
   allow(deck).to receive(:gets) {"99/99"}
   deck.get_player_input

   expect(deck.is_input_valid?(deck.player_input)).to be(false)
end

On my console, I get this:

...............A number you typed isn't even on the card...
[6, 6, 6, 6]
.

Finished in 0.026 seconds (files took 0.62704 seconds to load)
16 examples, 0 failures

The test passes but I don't want to see the bit. How do I get rid of it?

...............A number you typed isn't even on the card...
    [6, 6, 6, 6]
Ka Mok
  • 1,937
  • 3
  • 22
  • 47

1 Answers1

2

The best way is to silence the STDOUT stream in the test locally, in spec_helper.rb

def suppress_output
  allow(STDOUT).to receive(:puts) # this disables puts
end

in the test

RSpec.describe SomeClass do
  before do
    suppress_output
  end
end

if you are using Rails, use silence_stream.

it "returns false if input contains ANY numbers NOT from the current_card" do
  allow(deck).to receive(:gets) {"99/99"}
  silence_stream(STDOUT) do
     deck.get_player_input
     expect(deck.is_input_valid?(deck.player_input)).to be(false)
  end
end

Or globally (Rspec 3+)

RSpec.configure do |c|
  c.before { allow($stdout).to receive(:puts) }
end
gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45