User983
User983

Reputation: 11

Automated tests in RUBY. (No 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

end

So Ive got this code, what it basically does it create a CRUD (Create, Read, Update and Delete) system.

You can add only a name and an age corresponding to a user.

I have to now do automated testing for this code for each method.

I am not entirely sure how to get around and theres no proper tutorials for doing so.

So far ive got this to test the add method

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



class Test < Test::Unit::TestCase

    def test_add()

      add = Crud.new()

      assert_equal...

   end

end

Upvotes: 1

Views: 62

Answers (1)

Kris
Kris

Reputation: 19948

Try splitting the two concerns, model and view:

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

Allow stdin to be injected so that you can pass a double which will return a canned response in the tests:

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

Upvotes: 1

Related Questions