Forked from adomokos/stubbing_with_arguments_spec.rb
Created
December 14, 2011 19:28
-
-
Save cflipse/1478077 to your computer and use it in GitHub Desktop.
Stubbing with arguments - examples used in my "(More) Specific Stubbing with RSpec" blog post
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
class GivesCreditToPreferredCustomers | |
LOOK_BACK_PERIOD = 3 | |
def self.for_large_orders(sales_amount, added_credit) | |
# the has_large_purchases scope now takes two arguments | |
preferred_customers = Customer.has_large_purchases(sales_amount, LOOK_BACK_PERIOD) | |
preferred_customers.each do |customer| | |
customer.add_credit added_credit | |
end | |
end | |
end | |
class Customer | |
attr_reader :total_credit | |
def self.has_large_purchases(sales_amount) | |
puts "AR query to find buyers with large purchases" | |
end | |
def add_credit(amount) | |
@total_credit = 0 if @total_credit.nil? | |
@total_credit += amount | |
end | |
end | |
describe GivesCreditToPreferredCustomers do | |
context "for large orders" do | |
let(:sales_amount) { 10000 } | |
let(:credit_given) { 100 } | |
let(:found_customer) { Customer.new } | |
before do | |
Customer.stub(:has_large_purchases).and_return [found_customer] | |
end | |
it "update's the customer's credit" do | |
GivesCreditToPreferredCustomers \ | |
.for_large_orders(sales_amount, credit_given) | |
found_customer.total_credit.should == credit_given | |
end | |
it "scans over the past three units" do | |
look_back_period = 3 | |
# rspec will use the return value from an existing stub, if any | |
Customer.should_receive(:has_large_purchases).with(sales_amount, look_back_period) | |
GivesCreditToPreferredCustomers.for_large_orders(sales_amount, credit_given) | |
do | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment