Skip to content

Instantly share code, notes, and snippets.

@markfeedly
Created March 28, 2014 15:30
Show Gist options
  • Save markfeedly/9835469 to your computer and use it in GitHub Desktop.
Save markfeedly/9835469 to your computer and use it in GitHub Desktop.
Intro to testing using RSpec for 1-UP workshop
require 'rspec/autorun'
class WareHouse
attr_reader :quantity
def initialize
@quantity = 0
end
end
# RSpec
describe "Warehouse inventory" do
subject {WareHouse.new}
it "should start empty" do
expect(subject.quantity).to eq(0)
end
it "should have a quantity of 1 after adding an item" do
subject.add_item
expect(subject.quantity).to eq(1)
end
end
=begin
a worked example to read through or follow along trying it out
http://code.tutsplus.com/tutorials/ruby-for-newbies-testing-with-rspec--net-21297
some common matchers for you to play with
expect(subject).to be_true
expect(subject).to be_false
expect([]).to be_empty
expect({:foo => :bar}).to include(:foo => :bar)
primes = [1,2,3,5,7]
expect(primes).not_to include(4)
expect("Some string").to eq("Some string")
expect("Some string").not_to match(/foobar/)
# Message expectations. The expectation needs to be set up *before* the message is received
object = Object.new
expect(object).to receive(:do_stuff)
object.do_stuff
=end