Build Robust & Production Quality Applications - Lesson 1: First test in Rspec
23 Jan 2015- Add Rspec to your gemfile, make sure to include in both test and development:
group :test, :development do
gem 'rspec-rails'
end
- rails g rspec:install
Generates three items: 1. .rspec file - contains "color".. just means you want your output to be color-coded 2. spec folder 3. spec helper - where configuration goes
Create models folder inside spec folder
Create a todo_spec.rb within the models folder (name of model)
Within todo_spec.rb you must:
require 'spec_helper'
describe Todo do
it "saves itself" do
todo = Todo.new(name: "cook dinner", description: "love me some din din")
todo.save
Todo.first.name.should == "cook dinner"
end
end
** "it" is a keyword that begins each test
** "do" implements the test
** .should is another keyword will test to see if we have the desired result.
** == is the matcher
How to run rspec outside of Rails
- Create a project folder
- Inside the project folder, create a lib folder. Your code that you're testing will go inside here.
- Create another folder: spec, where your .spec test files will go.
- Make sure to require your code files inside your specs.