如何用 minitest 测试 `gets.chomp`
How to test `gets.chomp` with minitest
我的程序请求一个新的行星名称,得到响应,然后打印一条消息表明行星已创建。这是执行此操作的 Ruby 代码:
puts "Explore strange new worlds"
class CreateWorld
def initialize
puts 'what is the name of your planet?'
get_planet_name
new_planet_greeting
end
def get_planet_name
@name = gets.chomp
end
def new_planet_greeting
@name = @name.capitalize
puts "\n hi there I like the name of your planet, it's groovy baby \n"
puts "\n \"#{@name}\" will be a great addition to the universe \n"
puts "\n notifying all federations and starships about the creation of \"Planet #{@name}\""
end
end
myWorld = CreateWorld.new
如何测试我的代码?我检查规范中的用户输入。这是测试代码的最小规范:
require 'minitest/autorun'
require_relative 'createworld.rb'
describe "CreateWorld" do
before do
@World = CreateWorld.new
end
it "gets planet name" do
end
it "returns a new planet confirmation greeting" do
end
end
gets
和chomp
都内置在Ruby方法中,所以我不明白测试这些方法的意义,但如果你坚持这样做,那么既然他们在 get_planet_name
中调用,您可能想测试 get_planet_name
.
我的程序请求一个新的行星名称,得到响应,然后打印一条消息表明行星已创建。这是执行此操作的 Ruby 代码:
puts "Explore strange new worlds"
class CreateWorld
def initialize
puts 'what is the name of your planet?'
get_planet_name
new_planet_greeting
end
def get_planet_name
@name = gets.chomp
end
def new_planet_greeting
@name = @name.capitalize
puts "\n hi there I like the name of your planet, it's groovy baby \n"
puts "\n \"#{@name}\" will be a great addition to the universe \n"
puts "\n notifying all federations and starships about the creation of \"Planet #{@name}\""
end
end
myWorld = CreateWorld.new
如何测试我的代码?我检查规范中的用户输入。这是测试代码的最小规范:
require 'minitest/autorun'
require_relative 'createworld.rb'
describe "CreateWorld" do
before do
@World = CreateWorld.new
end
it "gets planet name" do
end
it "returns a new planet confirmation greeting" do
end
end
gets
和chomp
都内置在Ruby方法中,所以我不明白测试这些方法的意义,但如果你坚持这样做,那么既然他们在 get_planet_name
中调用,您可能想测试 get_planet_name
.