Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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