updating my Rspec knowledge

RSpec2 メモ

  • its が動かない
    • undefined method `its'

メモ

implicit subject & it

describe BlogPost do
  subject { BlogPost.new :title => 'foo', :body => 'bar' }

  it "sets published timestamp" do
    subject.publish!
    subject.published_on.should == Date.today
  end
end

its

describe BlogPost do
  subject do
    blog_post = BlogPost.new :title => 'foo', :body => 'bar'
    blog_post.publish!
    blog_post
  end

  it { should be_valid }
  its(:errors) { should be_empty }
  its(:title) { should == 'foo' }
  its(:body) { should == 'bar' }
  its(:published_on) { should == Date.today }
end

expect

# old one
lambda {
  BlogPost.create :title => 'Hello'
}.should change { BlogPost.count }.by(1)

# new one
expect {
  BlogPost.create :title => 'Hello'
}.to change { BlogPost.count }.by(1)

stubbing_chain

# old
BlogPost.stub(:recent => stub(:unpublished => stub(
  :chronological => [stub, stub, stub])))
# new
BlogPost.stub_chain(:recent, :unpublished, :chronological).
  and_return([stub, stub, stub])


let

# old one
before do
  @blog_post = BlogPost.create :title => 'Hello'
end

# new one
let(:blog_post) { BlogPost.create :title => 'Hello' }
# old
it "should be valid" do
  @user.should be_valid
end
# refactored
specify { @user.should be_valid }
# and more
describe User do
  it { should validate_presence_of :name }
  it { should have_one :address }
end


pending in before

# make all examples in a group pending
describe 'Veg-O-Matic' do
  before { pending }

shoulda

comes from the land of Test::Unit
powerful/handy/high-level macros
many matchers now available in RSpec
validate_presence_of, validate_format_of, ensure_length_of, have_many...
  • spec/support フォルダに置いておけば読み込まれる?
    • spec_helper を汚さなくてよい
    • r g rspec でのアップグレード対応
  • bundle open gem_name 素敵すぎる