Skip to content

Instantly share code, notes, and snippets.

@LupineDev
Created April 21, 2012 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LupineDev/2433757 to your computer and use it in GitHub Desktop.
Save LupineDev/2433757 to your computer and use it in GitHub Desktop.
rspec mock/stub example
describe Order do
describe "#sub_total" do
# writing everything to db
it "should calculate the sum of all of its shop_items" do
order = Order.create(some_options)
3.times do
order.shop_items << ShopItem.create(:price => 2.00)
end
order.subtotal.should be_within(0.01).of(6.0)
end
# mocking
it "should calculate the sum of all of its shop_items" do
order = Order.create(some_options)
fake_items = [stub(:price => 2.00), stub(:price => 2.00), stub(:price => 2.00)]
order.stub!(:shop_items).and_return(fake_items)
order.subtotal.should be_within(0.01).of(6.0)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment