Skip to content

Instantly share code, notes, and snippets.

@data-doge
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save data-doge/c55fcd7a7c0693662b9e to your computer and use it in GitHub Desktop.

Select an option

Save data-doge/c55fcd7a7c0693662b9e to your computer and use it in GitHub Desktop.
class Console
def render_help!
puts "available commands: "
puts "list"
puts "add <description>"
puts "delete <id>"
puts "tick <id>"
puts "untick <id>"
end
def render_all!(tasks)
puts "your list"
puts tasks
end
def task_successfully_added!(task)
puts "task: '#{task.description}' was added to the DB!"
end
def task_not_added!
puts "error: task must have a description"
end
end
docs
google: relish rspec
- check the version at the top of the page
- rspec 2.x is very different from rspec 3.x
installation + config
spec_helper: this is a file that lives in your ./spec folder, anything that all your tests need, put here.
spec organization:
describe
- used to organize tests about the same thing
context
- used to organize tests about the same thing, but in a different context
it
- actual tests
expectations:
relish-rspec expectations (3.x)
- expect(<actual>).to <matcher>(<expected>)
cute tricks to refactor our tests:
before
- the contents of a before block run before every test in a describe
after
- the contents of an after block run after every test in a describe
let
- generates something when we talk about it
- that something is cached within tests
- not cached across tests
let!
- generates something before every test
require 'rubygems'
require File.expand_path("../../config/application", __FILE__)
class Task < ActiveRecord::Base
validates :description, presence: true
def to_s
"[#{marker}] #{id}. #{description}"
end
def marker
completed ? "X" : " "
end
end
require 'spec_helper'
describe "Task" do
let(:incomplete_task) { Task.create(description: "meow") }
let(:complete_task) { Task.create(description: "meow", completed: true) }
before do
Task.destroy_all
end
describe "#to_s" do
it "prints the task in a pretty way" do
expect(incomplete_task.to_s).to eq("[ ] #{incomplete_task.id}. #{incomplete_task.description}")
end
end
describe "#marker" do
context "if completed" do
it "returns 'X'" do
expect(complete_task.marker).to eq("X")
end
end
context "if incompleted" do
it "returns ' '" do
expect(incomplete_task.marker).to eq(" ")
end
end
end
end
require_relative '../../config/application.rb'
require_relative '../../app/views/console.rb'
class TasksController
def initialize
@console = Console.new
end
def print_list!
tasks = Task.all
@console.render_all!(tasks)
end
def add_to_list!(description)
task = Task.new(description: description)
if task.save
@console.task_successfully_added!(task)
else
@console.task_not_added!
end
end
def help!
@console.render_help!
end
end
require 'spec_helper'
require_relative './../app/controllers/tasks_controller.rb'
describe "TasksController" do
let(:tasks_controller) { TasksController.new }
before do
Task.destroy_all
end
describe "#add_to_list!(description)" do
context "if description" do
it "adds new task with specified description to the DB" do
tasks_controller.add_to_list!("meow")
expect(Task.find_by_description("meow")).to be_truthy
end
end
context "if no description" do
it "task not added to the DB" do
tasks_controller.add_to_list!("")
expect(Task.all.length).to eq(0)
end
end
end
end
require_relative 'config/application'
require_relative 'app/controllers/tasks_controller.rb'
user_input = ARGV
command = user_input.shift
params = user_input.join(" ")
tasks_controller = TasksController.new
case command
when "list" then tasks_controller.print_list!
when "add" then tasks_controller.add_to_list!(params)
else tasks_controller.help!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment