$ gem install rspec
$ rspec --v
$ rspec --help
$ rspec --init
create spec/spec_helper.rb
create .rspec
- * *
Edit .rspec ( use: "ls -la" to see the hidden files)
--color
--format documentation
-
$ cd spec $ cat spec_helper.rb
-
# This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only # loaded once. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = 'random' end
#2) Write basic describe/it blocks to test an existing Class
describe Square do
it "has a side attribute" do
test code
end
it "has an area method" do
test code
end
end
-
before(:each) do @my_square = Square.new(5) end after(:each) do @my_square = nil end
class Square
attr_accessor :side
def initialize(side)
@side = side
end
def area
@side ** 2
end
def perimeter
@side * 4
end
def to_s
"I am a square. My sides are of length: #{@side}. My area is: #{self.area}, and my perimeter is: #{self.perimeter}"
end
end
require './square'
require 'spec_helper'
describe Square do
before(:each) do
@my_square = Square.new(5)
end
it "should have a side" do
my_square = Square.new(5)
@my_square.side.should eq(5)
end
end
require './employee'
require 'spec_helper'
describe Employee do
before(:each) do
@employee = Employee.new("YourName")
end
it "has a name " do
@employee.name.should eq("YourName")
end
it "can change the name" do
@employee.name = "NewName"
@employee.name.should eq("YourName")
end
end
class Employee
attr_accessor :name
def initialize(name)
@name = name
end
end
require './employee'
require 'spec_helper'
describe Employee do
before(:each) do
@employee = Employee.new("YourName")
end
it "has a name " do
@employee.name.should eq("YourName")
end
it "can change the name" do
@employee.name = "NewName"
@employee.name.should eq("YourName")
end
end
list of commands like eq
should vs. expect