2016-09-15 46 views
1
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 

RUBY的自動測試。 (無Rails)的

所以Ive得到這個代碼,它基本上不會創建一個CRUD(創建,讀取,更新和刪除)系統。

您只能添加與用戶對應的名稱和年齡。

我現在必須對每種方法的代碼進行自動測試。

我不完全確定如何解決問題,沒有適當的教程來做這件事。

到目前爲止,香港專業教育學院得到這個測試add方法

require "okok.rb" 
require "test/unit" 



class Test < Test::Unit::TestCase 

    def test_add() 

     add = Crud.new() 

     assert_equal... 

    end 

+0

試試這個https://learnrubythehardway.org/book/ex47.html – aledustet

+0

它有點令人困惑,一個 – User983

+0

這將是很難測試,因爲'gets'這將等待用戶輸入。我所建議的是將課堂分爲兩個「純」類,它接受輸入併產生輸出,這是可測試的。然後有第二個類,它實際上是包含'gets'和'puts'的用戶界面,調用第一個類來完成實際的工作。 – Kris

回答

1

嘗試分裂的兩個問題,模型和視圖:

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 

允許標準輸入將被注入,使你可以通過一個雙將返回在測試中的罐頭響應:

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 
+0

會有更簡單的解決方案嗎? 我只是一個初學者,似乎無法準確理解如何將它們實現爲2個類並在其上工作。 – User983

+0

不是我所知道的,因爲'gets'會一直等到用戶按下輸入,用*自動*測試如何模擬用戶輸入?同樣'puts'將數據發送到標準輸出,如何測試讀取這個?沒有簡單的答案。可能會留下自動化測試,直到您學習了更多的Ruby和麪向對象的校長。 – Kris