Testing recursive IO prompt in rspec2019 Community Moderator ElectionHow do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?How do you unit test private methods?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Unit tests vs Functional testsWriting unit tests in Python: How do I start?RSpec - generic failure message instead of useful outputError after rake test, ruby on rails
Are small insurances worth it?
Trocar background-image com delay via jQuery
Translation of 答えを知っている人はいませんでした
What passages of Adi Shankaracharya’s Brahmasutra Bhashya does Sudarshana Suri cite?
What is the "determinant" of two vectors?
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Did Amazon pay $0 in taxes last year?
Should we avoid writing fiction about historical events without extensive research?
How should I solve this integral with changing parameters?
What will happen if my luggage gets delayed?
Writing text next to a table
Are all players supposed to be able to see each others' character sheets?
Playing a 7-string guitar song on a 6-string guitar
Rationale to prefer local variables over instance variables?
How do I increase the number of TTY consoles?
How do we create new idioms and use them in a novel?
Cycles on the torus
Was it really inappropriate to write a pull request for the company I interviewed with?
Do Paladin Auras of Differing Oaths Stack?
Giving a career talk in my old university, how prominently should I tell students my salary?
Is it appropriate to ask a former professor to order a book for me through an inter-library loan?
Does the US political system, in principle, allow for a no-party system?
"If + would" conditional in present perfect tense
Having the player face themselves after the mid-game
Testing recursive IO prompt in rspec
2019 Community Moderator ElectionHow do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?How do you unit test private methods?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Unit tests vs Functional testsWriting unit tests in Python: How do I start?RSpec - generic failure message instead of useful outputError after rake test, ruby on rails
I am writing a connect-four game for more OO and rspec practice. As part of my program, I would like to prompt the user to choose a column that they would like to put their game piece in. Here's that method:
def get_col_choice(input, output)
output.print "Player #@player, choose a column from 0-6: "
input_string = input.gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
output.puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice(input, output)
end
In IRB, everything works as I planned. My hangup is in rspec where I am faced with a NoMethodError, which I think is coming from my recursive call to get_col_choice
.
Can anyone help me understand what I can do to either improve get_col_choice
or write the correct tests in Rspec? This is what my console output from my rspec file looks like:
Failures:
1) ConnectFourGame#get_col_choice notifies users which player gets to choose a column
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:52:in `block (3 levels) in <top (required)>'
2) ConnectFourGame#get_col_choice returns the player's column choice
Failure/Error: expect($connect_four.get_col_choice(input, output)).to eq(0)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:57:in `block (3 levels) in <top (required)>'
3) ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
Failure/Error: expect(output.string).to include("The column you chose is fully occupied.")
expected "" to include "The column you chose is fully occupied."
# ./connect_four_game_spec.rb:62:in `block (3 levels) in <top (required)>'
4) ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:67:in `block (3 levels) in <top (required)>'
5) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:74:in `block (4 levels) in <top (required)>'
6) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:80:in `block (4 levels) in <top (required)>'
Finished in 0.02399 seconds (files took 0.1108 seconds to load)
12 examples, 6 failures
Failed examples:
rspec ./connect_four_game_spec.rb:51 # ConnectFourGame#get_col_choice notifies users which player gets to choose a column
rspec ./connect_four_game_spec.rb:56 # ConnectFourGame#get_col_choice returns the player's column choice
rspec ./connect_four_game_spec.rb:60 # ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
rspec ./connect_four_game_spec.rb:66 # ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
rspec ./connect_four_game_spec.rb:73 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
rspec ./connect_four_game_spec.rb:79 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Here are the tests I wrote for get_col_choice
:
describe '#get_col_choice' do
let(:output) StringIO.new
let(:input) StringIO.new("0n")
it 'notifies users which player gets to choose a column' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Player 2, choose a column from 0-6: ")
end
it "returns the player's column choice" do
expect($connect_four.get_col_choice(input, output)).to eq(0)
end
it 'notifies the user if the column they chose is already full of pieces' do
6.times $connect_four.board.add_game_piece_to(0, "u2468")
expect(output.string).to include("The column you chose is fully occupied.")
end
let(:input) StringIO.new("!n")
it 'notifies the user their input is invalid when a non-numeric string is entered' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Your choice of column is invalid. Try again.")
end
context 'column choice is out of bounds' do
let(:input) StringIO.new("7n")
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column you chose is out of bounds.")
end
let(:input) StringIO.new("-1n")
it 'notifies the user their column choice is out of bounds when col is less than 0' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column number you chose is out of bounds.")
end
end
end
ruby unit-testing testing tdd rspec3
New contributor
add a comment |
I am writing a connect-four game for more OO and rspec practice. As part of my program, I would like to prompt the user to choose a column that they would like to put their game piece in. Here's that method:
def get_col_choice(input, output)
output.print "Player #@player, choose a column from 0-6: "
input_string = input.gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
output.puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice(input, output)
end
In IRB, everything works as I planned. My hangup is in rspec where I am faced with a NoMethodError, which I think is coming from my recursive call to get_col_choice
.
Can anyone help me understand what I can do to either improve get_col_choice
or write the correct tests in Rspec? This is what my console output from my rspec file looks like:
Failures:
1) ConnectFourGame#get_col_choice notifies users which player gets to choose a column
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:52:in `block (3 levels) in <top (required)>'
2) ConnectFourGame#get_col_choice returns the player's column choice
Failure/Error: expect($connect_four.get_col_choice(input, output)).to eq(0)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:57:in `block (3 levels) in <top (required)>'
3) ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
Failure/Error: expect(output.string).to include("The column you chose is fully occupied.")
expected "" to include "The column you chose is fully occupied."
# ./connect_four_game_spec.rb:62:in `block (3 levels) in <top (required)>'
4) ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:67:in `block (3 levels) in <top (required)>'
5) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:74:in `block (4 levels) in <top (required)>'
6) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:80:in `block (4 levels) in <top (required)>'
Finished in 0.02399 seconds (files took 0.1108 seconds to load)
12 examples, 6 failures
Failed examples:
rspec ./connect_four_game_spec.rb:51 # ConnectFourGame#get_col_choice notifies users which player gets to choose a column
rspec ./connect_four_game_spec.rb:56 # ConnectFourGame#get_col_choice returns the player's column choice
rspec ./connect_four_game_spec.rb:60 # ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
rspec ./connect_four_game_spec.rb:66 # ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
rspec ./connect_four_game_spec.rb:73 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
rspec ./connect_four_game_spec.rb:79 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Here are the tests I wrote for get_col_choice
:
describe '#get_col_choice' do
let(:output) StringIO.new
let(:input) StringIO.new("0n")
it 'notifies users which player gets to choose a column' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Player 2, choose a column from 0-6: ")
end
it "returns the player's column choice" do
expect($connect_four.get_col_choice(input, output)).to eq(0)
end
it 'notifies the user if the column they chose is already full of pieces' do
6.times $connect_four.board.add_game_piece_to(0, "u2468")
expect(output.string).to include("The column you chose is fully occupied.")
end
let(:input) StringIO.new("!n")
it 'notifies the user their input is invalid when a non-numeric string is entered' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Your choice of column is invalid. Try again.")
end
context 'column choice is out of bounds' do
let(:input) StringIO.new("7n")
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column you chose is out of bounds.")
end
let(:input) StringIO.new("-1n")
it 'notifies the user their column choice is out of bounds when col is less than 0' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column number you chose is out of bounds.")
end
end
end
ruby unit-testing testing tdd rspec3
New contributor
add a comment |
I am writing a connect-four game for more OO and rspec practice. As part of my program, I would like to prompt the user to choose a column that they would like to put their game piece in. Here's that method:
def get_col_choice(input, output)
output.print "Player #@player, choose a column from 0-6: "
input_string = input.gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
output.puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice(input, output)
end
In IRB, everything works as I planned. My hangup is in rspec where I am faced with a NoMethodError, which I think is coming from my recursive call to get_col_choice
.
Can anyone help me understand what I can do to either improve get_col_choice
or write the correct tests in Rspec? This is what my console output from my rspec file looks like:
Failures:
1) ConnectFourGame#get_col_choice notifies users which player gets to choose a column
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:52:in `block (3 levels) in <top (required)>'
2) ConnectFourGame#get_col_choice returns the player's column choice
Failure/Error: expect($connect_four.get_col_choice(input, output)).to eq(0)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:57:in `block (3 levels) in <top (required)>'
3) ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
Failure/Error: expect(output.string).to include("The column you chose is fully occupied.")
expected "" to include "The column you chose is fully occupied."
# ./connect_four_game_spec.rb:62:in `block (3 levels) in <top (required)>'
4) ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:67:in `block (3 levels) in <top (required)>'
5) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:74:in `block (4 levels) in <top (required)>'
6) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:80:in `block (4 levels) in <top (required)>'
Finished in 0.02399 seconds (files took 0.1108 seconds to load)
12 examples, 6 failures
Failed examples:
rspec ./connect_four_game_spec.rb:51 # ConnectFourGame#get_col_choice notifies users which player gets to choose a column
rspec ./connect_four_game_spec.rb:56 # ConnectFourGame#get_col_choice returns the player's column choice
rspec ./connect_four_game_spec.rb:60 # ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
rspec ./connect_four_game_spec.rb:66 # ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
rspec ./connect_four_game_spec.rb:73 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
rspec ./connect_four_game_spec.rb:79 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Here are the tests I wrote for get_col_choice
:
describe '#get_col_choice' do
let(:output) StringIO.new
let(:input) StringIO.new("0n")
it 'notifies users which player gets to choose a column' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Player 2, choose a column from 0-6: ")
end
it "returns the player's column choice" do
expect($connect_four.get_col_choice(input, output)).to eq(0)
end
it 'notifies the user if the column they chose is already full of pieces' do
6.times $connect_four.board.add_game_piece_to(0, "u2468")
expect(output.string).to include("The column you chose is fully occupied.")
end
let(:input) StringIO.new("!n")
it 'notifies the user their input is invalid when a non-numeric string is entered' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Your choice of column is invalid. Try again.")
end
context 'column choice is out of bounds' do
let(:input) StringIO.new("7n")
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column you chose is out of bounds.")
end
let(:input) StringIO.new("-1n")
it 'notifies the user their column choice is out of bounds when col is less than 0' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column number you chose is out of bounds.")
end
end
end
ruby unit-testing testing tdd rspec3
New contributor
I am writing a connect-four game for more OO and rspec practice. As part of my program, I would like to prompt the user to choose a column that they would like to put their game piece in. Here's that method:
def get_col_choice(input, output)
output.print "Player #@player, choose a column from 0-6: "
input_string = input.gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
output.puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice(input, output)
end
In IRB, everything works as I planned. My hangup is in rspec where I am faced with a NoMethodError, which I think is coming from my recursive call to get_col_choice
.
Can anyone help me understand what I can do to either improve get_col_choice
or write the correct tests in Rspec? This is what my console output from my rspec file looks like:
Failures:
1) ConnectFourGame#get_col_choice notifies users which player gets to choose a column
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:52:in `block (3 levels) in <top (required)>'
2) ConnectFourGame#get_col_choice returns the player's column choice
Failure/Error: expect($connect_four.get_col_choice(input, output)).to eq(0)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:57:in `block (3 levels) in <top (required)>'
3) ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
Failure/Error: expect(output.string).to include("The column you chose is fully occupied.")
expected "" to include "The column you chose is fully occupied."
# ./connect_four_game_spec.rb:62:in `block (3 levels) in <top (required)>'
4) ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:67:in `block (3 levels) in <top (required)>'
5) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:74:in `block (4 levels) in <top (required)>'
6) ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Failure/Error: $connect_four.get_col_choice(input, output)
NoMethodError:
undefined method `chomp' for nil:NilClass
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:31:in `get_col_choice'
# /home/learnsometing/Desktop/the_Odin_Project/ruby/connect-four/lib/connect_four_game.rb:42:in `get_col_choice'
# ./connect_four_game_spec.rb:80:in `block (4 levels) in <top (required)>'
Finished in 0.02399 seconds (files took 0.1108 seconds to load)
12 examples, 6 failures
Failed examples:
rspec ./connect_four_game_spec.rb:51 # ConnectFourGame#get_col_choice notifies users which player gets to choose a column
rspec ./connect_four_game_spec.rb:56 # ConnectFourGame#get_col_choice returns the player's column choice
rspec ./connect_four_game_spec.rb:60 # ConnectFourGame#get_col_choice notifies the user if the column they chose is already full of pieces
rspec ./connect_four_game_spec.rb:66 # ConnectFourGame#get_col_choice notifies the user their input is invalid when a non-numeric string is entered
rspec ./connect_four_game_spec.rb:73 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is greater than 6
rspec ./connect_four_game_spec.rb:79 # ConnectFourGame#get_col_choice column choice is out of bounds notifies the user their column choice is out of bounds when col is less than 0
Here are the tests I wrote for get_col_choice
:
describe '#get_col_choice' do
let(:output) StringIO.new
let(:input) StringIO.new("0n")
it 'notifies users which player gets to choose a column' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Player 2, choose a column from 0-6: ")
end
it "returns the player's column choice" do
expect($connect_four.get_col_choice(input, output)).to eq(0)
end
it 'notifies the user if the column they chose is already full of pieces' do
6.times $connect_four.board.add_game_piece_to(0, "u2468")
expect(output.string).to include("The column you chose is fully occupied.")
end
let(:input) StringIO.new("!n")
it 'notifies the user their input is invalid when a non-numeric string is entered' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("Your choice of column is invalid. Try again.")
end
context 'column choice is out of bounds' do
let(:input) StringIO.new("7n")
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column you chose is out of bounds.")
end
let(:input) StringIO.new("-1n")
it 'notifies the user their column choice is out of bounds when col is less than 0' do
$connect_four.get_col_choice(input, output)
expect(output.string).to include("The column number you chose is out of bounds.")
end
end
end
ruby unit-testing testing tdd rspec3
ruby unit-testing testing tdd rspec3
New contributor
New contributor
edited Mar 7 at 0:03
Supa Mega Ducky Momo da Waffle
2,00451129
2,00451129
New contributor
asked Mar 6 at 22:13
Brian MonaccioBrian Monaccio
63
63
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I was able to solve my problem by removing the input
and output
from get_col_choice
:
def get_col_choice
print "Player #@player, choose a column from 0-6: "
input_string = gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if @board.unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice
end
In my rspec file, instead of using StringIO
to inject the behavior I expected into get_col_choice
, I stubbed gets
to return the values I needed to trigger expected behaviors:
#code left out for brevity
before(:each) do
@game = ConnectFourGame.new
end
describe '#get_col_choice' do
before(:each) do
allow($stdout).to receive(:write)
@game.player = 1
end
context 'valid input' do
before(:each) do
allow(@game).to receive(:gets) "0n"
end
it 'notifies users which player gets to choose a column' do
expect @game.get_col_choice .to output('Player 1, choose a column from 0-6: ').to_stdout
end
it "returns the user's column choice as an integer" do
expect(@game.get_col_choice).to eq(0)
end
end
context 'invalid input' do
output_expectation = Proc.new expect @game.get_col_choice .to output(output).to_stdout
it 'notifies the user if the column they chose is already full of pieces' do
allow(@game).to receive(:gets).and_return("0n", "1n")
6.times @game.board.add_game_piece_to(0, "u2648")
output = "Player 1, choose a column from 0-6: The column you chose is fully occupied.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their input is invalid when a non-numeric string is entered' do
allow(@game).to receive(:gets).and_return("foon", "0n")
output = "Player 1, choose a column from 0-6: Your choice of column is invalid. Try again.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
allow(@game).to receive(:gets).and_return("7n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is less than 0' do
allow(@game).to receive(:gets).and_return("-1n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
end
end
The reason I received errors in my previous implementation of get_col_choice
was because after StringIO
received my first input string, it closed, resulting in a value of nil
that I tried to call gets
on.
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Brian Monaccio is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033020%2ftesting-recursive-io-prompt-in-rspec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I was able to solve my problem by removing the input
and output
from get_col_choice
:
def get_col_choice
print "Player #@player, choose a column from 0-6: "
input_string = gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if @board.unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice
end
In my rspec file, instead of using StringIO
to inject the behavior I expected into get_col_choice
, I stubbed gets
to return the values I needed to trigger expected behaviors:
#code left out for brevity
before(:each) do
@game = ConnectFourGame.new
end
describe '#get_col_choice' do
before(:each) do
allow($stdout).to receive(:write)
@game.player = 1
end
context 'valid input' do
before(:each) do
allow(@game).to receive(:gets) "0n"
end
it 'notifies users which player gets to choose a column' do
expect @game.get_col_choice .to output('Player 1, choose a column from 0-6: ').to_stdout
end
it "returns the user's column choice as an integer" do
expect(@game.get_col_choice).to eq(0)
end
end
context 'invalid input' do
output_expectation = Proc.new expect @game.get_col_choice .to output(output).to_stdout
it 'notifies the user if the column they chose is already full of pieces' do
allow(@game).to receive(:gets).and_return("0n", "1n")
6.times @game.board.add_game_piece_to(0, "u2648")
output = "Player 1, choose a column from 0-6: The column you chose is fully occupied.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their input is invalid when a non-numeric string is entered' do
allow(@game).to receive(:gets).and_return("foon", "0n")
output = "Player 1, choose a column from 0-6: Your choice of column is invalid. Try again.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
allow(@game).to receive(:gets).and_return("7n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is less than 0' do
allow(@game).to receive(:gets).and_return("-1n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
end
end
The reason I received errors in my previous implementation of get_col_choice
was because after StringIO
received my first input string, it closed, resulting in a value of nil
that I tried to call gets
on.
New contributor
add a comment |
I was able to solve my problem by removing the input
and output
from get_col_choice
:
def get_col_choice
print "Player #@player, choose a column from 0-6: "
input_string = gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if @board.unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice
end
In my rspec file, instead of using StringIO
to inject the behavior I expected into get_col_choice
, I stubbed gets
to return the values I needed to trigger expected behaviors:
#code left out for brevity
before(:each) do
@game = ConnectFourGame.new
end
describe '#get_col_choice' do
before(:each) do
allow($stdout).to receive(:write)
@game.player = 1
end
context 'valid input' do
before(:each) do
allow(@game).to receive(:gets) "0n"
end
it 'notifies users which player gets to choose a column' do
expect @game.get_col_choice .to output('Player 1, choose a column from 0-6: ').to_stdout
end
it "returns the user's column choice as an integer" do
expect(@game.get_col_choice).to eq(0)
end
end
context 'invalid input' do
output_expectation = Proc.new expect @game.get_col_choice .to output(output).to_stdout
it 'notifies the user if the column they chose is already full of pieces' do
allow(@game).to receive(:gets).and_return("0n", "1n")
6.times @game.board.add_game_piece_to(0, "u2648")
output = "Player 1, choose a column from 0-6: The column you chose is fully occupied.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their input is invalid when a non-numeric string is entered' do
allow(@game).to receive(:gets).and_return("foon", "0n")
output = "Player 1, choose a column from 0-6: Your choice of column is invalid. Try again.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
allow(@game).to receive(:gets).and_return("7n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is less than 0' do
allow(@game).to receive(:gets).and_return("-1n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
end
end
The reason I received errors in my previous implementation of get_col_choice
was because after StringIO
received my first input string, it closed, resulting in a value of nil
that I tried to call gets
on.
New contributor
add a comment |
I was able to solve my problem by removing the input
and output
from get_col_choice
:
def get_col_choice
print "Player #@player, choose a column from 0-6: "
input_string = gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if @board.unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice
end
In my rspec file, instead of using StringIO
to inject the behavior I expected into get_col_choice
, I stubbed gets
to return the values I needed to trigger expected behaviors:
#code left out for brevity
before(:each) do
@game = ConnectFourGame.new
end
describe '#get_col_choice' do
before(:each) do
allow($stdout).to receive(:write)
@game.player = 1
end
context 'valid input' do
before(:each) do
allow(@game).to receive(:gets) "0n"
end
it 'notifies users which player gets to choose a column' do
expect @game.get_col_choice .to output('Player 1, choose a column from 0-6: ').to_stdout
end
it "returns the user's column choice as an integer" do
expect(@game.get_col_choice).to eq(0)
end
end
context 'invalid input' do
output_expectation = Proc.new expect @game.get_col_choice .to output(output).to_stdout
it 'notifies the user if the column they chose is already full of pieces' do
allow(@game).to receive(:gets).and_return("0n", "1n")
6.times @game.board.add_game_piece_to(0, "u2648")
output = "Player 1, choose a column from 0-6: The column you chose is fully occupied.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their input is invalid when a non-numeric string is entered' do
allow(@game).to receive(:gets).and_return("foon", "0n")
output = "Player 1, choose a column from 0-6: Your choice of column is invalid. Try again.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
allow(@game).to receive(:gets).and_return("7n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is less than 0' do
allow(@game).to receive(:gets).and_return("-1n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
end
end
The reason I received errors in my previous implementation of get_col_choice
was because after StringIO
received my first input string, it closed, resulting in a value of nil
that I tried to call gets
on.
New contributor
I was able to solve my problem by removing the input
and output
from get_col_choice
:
def get_col_choice
print "Player #@player, choose a column from 0-6: "
input_string = gets.chomp
begin
col_choice = Integer(input_string)
raise("The column you chose is out of bounds.") if out_of_bounds?(col_choice)
raise("The column you chose is fully occupied.") if @board.unavailable?(col_choice)
return col_choice
rescue TypeError, ArgumentError
puts "Your choice of column is invalid. Try again."
rescue RuntimeError => err
puts err.message
end
get_col_choice
end
In my rspec file, instead of using StringIO
to inject the behavior I expected into get_col_choice
, I stubbed gets
to return the values I needed to trigger expected behaviors:
#code left out for brevity
before(:each) do
@game = ConnectFourGame.new
end
describe '#get_col_choice' do
before(:each) do
allow($stdout).to receive(:write)
@game.player = 1
end
context 'valid input' do
before(:each) do
allow(@game).to receive(:gets) "0n"
end
it 'notifies users which player gets to choose a column' do
expect @game.get_col_choice .to output('Player 1, choose a column from 0-6: ').to_stdout
end
it "returns the user's column choice as an integer" do
expect(@game.get_col_choice).to eq(0)
end
end
context 'invalid input' do
output_expectation = Proc.new expect @game.get_col_choice .to output(output).to_stdout
it 'notifies the user if the column they chose is already full of pieces' do
allow(@game).to receive(:gets).and_return("0n", "1n")
6.times @game.board.add_game_piece_to(0, "u2648")
output = "Player 1, choose a column from 0-6: The column you chose is fully occupied.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their input is invalid when a non-numeric string is entered' do
allow(@game).to receive(:gets).and_return("foon", "0n")
output = "Player 1, choose a column from 0-6: Your choice of column is invalid. Try again.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is greater than 6' do
allow(@game).to receive(:gets).and_return("7n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
it 'notifies the user their column choice is out of bounds when col is less than 0' do
allow(@game).to receive(:gets).and_return("-1n", "0n")
output = "Player 1, choose a column from 0-6: The column you chose is out of bounds.nPlayer 1, choose a column from 0-6: "
output_expectation
end
end
end
The reason I received errors in my previous implementation of get_col_choice
was because after StringIO
received my first input string, it closed, resulting in a value of nil
that I tried to call gets
on.
New contributor
New contributor
answered 2 days ago
Brian MonaccioBrian Monaccio
63
63
New contributor
New contributor
add a comment |
add a comment |
Brian Monaccio is a new contributor. Be nice, and check out our Code of Conduct.
Brian Monaccio is a new contributor. Be nice, and check out our Code of Conduct.
Brian Monaccio is a new contributor. Be nice, and check out our Code of Conduct.
Brian Monaccio is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033020%2ftesting-recursive-io-prompt-in-rspec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown