简体   繁体   中英

How to test `gets.chomp` with minitest

My program asks for a new planet name, gets a response, and then prints a message that the planet has been created. Here is the Ruby code that does this:

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 

How do I test my code? I check for user input within the spec. Here is the minitest spec that tests the code:

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

这两种getschomp是建立在Ruby方法,所以我不明白这些测试方法的地步,但如果你坚持这样做,那么因为他们被称为在get_planet_name ,你可能想测试get_planet_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM