用RUBY进行自动化测试.(无Rails)


class Crud
 $people = { }
 def self.add
    puts "Enter the name of the person you would like to add in the database."
    name = gets.chomp.to_s
    if  $people[name].nil?
        puts "What is #{name}'s age?"
        age = gets.chomp.to_i
        $people[name] = age.to_i
        puts "#{name} has been succesfully added to the database."
    else
        puts "#{name} already exists in the database. Try again!"
        Crud.add
        puts $people
    end
 end
 def self.delete
    puts "Who would you like to delete from the database? "
    count = 1
    $people.each do     |name, age|
        puts "#{count})  #{name}: #{age}"
        count += 1
    end
    name = gets.chomp.to_s
    if $people[name.to_s].nil?
        puts "User not found. Try again!"
    else
        $people.delete(name)
        puts "#{name} has been deleted from the database."
        puts "This is the updated database: "
        count = 1
        $people.each do     |name, age|
            puts "#{count})  #{name}: #{age}"
            count += 1
        end
    end
 end
 def self.read
    puts "Updated database:"
    puts "-----------------"
    count = 1
    $people.each do     |name, age|
        puts "#{count})  #{name}: #{age}"
        count += 1
    end
    puts "-----------------"
 end
 def self.update
    puts "Who would you like to update?"
    count = 1
    $people.each do     |name, age|
        puts "#{count})  #{name}: #{age}"
        count += 1
    end
    name = gets.chomp.to_s
    if $people[name].nil?
        puts "#{name} not found in the database. Try again."
        update
    else 
        puts "Type in a new age for #{name} "
        age = gets.chomp.to_i
        $people[name] = age
        puts "#{name}'s age has been updated to #{age}"
        puts "This is the updated database: "
        count = 1
        $people.each do     |name, age|
            puts "#{count})  #{name}: #{age}"
            count += 1
        end
    end
 end
 flag = true
 while flag == true
    puts "What would you like to do?"
    puts "1. Add,  2. Update,  3. Delete,  4. Read"
    choice = gets.chomp
    if choice == "1" || choice == "add"
        Crud.add
    elsif choice == "2" || choice == "update"
        Crud.update
    elsif choice == "3" || choice == "delete"
        Crud.delete
    elsif choice == "4" || choice == "read"
        Crud.read
    elsif choice == "cancel"
        puts "Program has ended"
        flag = false
    else 
        puts "ERROR!! Try again"
    end
 end

结束

我得到了这段代码,它基本上做的是创建一个CRUD(创建,读取,更新和删除)系统。

只能添加用户对应的姓名和年龄。

我现在必须为每个方法对这段代码进行自动测试。

我不完全确定如何四处走动,也没有合适的教程。

到目前为止,我们用这个来测试add方法
require "okok.rb"
require "test/unit"

class Test < Test::Unit::TestCase
    def test_add()
      add = Crud.new()
      assert_equal...
   end
结束

尝试拆分两个关注点,模型和视图:

class People
  def initialize
    @people = []
  end
  def add(person)
    @people << person
  end
  def delete(id)
    @people.delete(find(id))
  end
  def find(id)
    @people.find { |p| p[:id] }
  end
end

允许stdin被注入,这样你就可以在测试中传递一个双精度值,它将返回一个固定的响应:

class PeopleUI
  def initialize(dependencies = {})
    @stdin = dependencies.fetch(:stdin, STDIN)
    @people = dependencies.fetch(:people, People.new)
  end
  def add
    puts "What is the name?"
    name = @stdin.gets
    @people.add({ name: name })
  end
end

最新更新