繁体   English   中英

如何用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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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