Skip to content

Instantly share code, notes, and snippets.

@kornypoet
Created June 22, 2012 18:24
Show Gist options
  • Save kornypoet/2974384 to your computer and use it in GitHub Desktop.
Save kornypoet/2974384 to your computer and use it in GitHub Desktop.
RSpec Examples
#run me with `rspec -fd --color rspec_examples.rb`
require 'rspec'
# The class we want to test
class YoMama
attr_accessor :makeup
def fat?() true ; end
def wearing?(what)
what == 'combat boots' ? true : false
end
def put(something)
@makeup = something.match(/head/) ? 'mind' : 'body'
self
end
end
describe YoMama do
# Subject can be defined with a block
# The following is actually implicit because of the describe block
subject { YoMama.new }
it 'needs to lose weight' do
# Automatic matchers are written like this
subject.should be_fat
# This could be written badly this way
subject.fat?.should be_true
# Or this way, which is even worse
subject.fat?.should == true
end
it 'dresses like a man' do
# Automatic matchers with args
subject.should be_wearing('combat boots')
# should_not asserts false statements
subject.should_not be_wearing('a bikini')
end
it 'is uneducated' do
# You can define your own matchers; make your tests readable!
subject.put('lipstick on her head').should makeup_her('mind')
end
end
# Here is where we defined the matcher for the last example
RSpec::Matchers.define :makeup_her do |expected|
match do |actual|
actual.makeup == expected
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment