Created
April 21, 2012 03:51
-
-
Save LupineDev/2433757 to your computer and use it in GitHub Desktop.
rspec mock/stub example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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