Skip to content

Instantly share code, notes, and snippets.

@adomokos
Created December 13, 2011 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adomokos/1473661 to your computer and use it in GitHub Desktop.
Save adomokos/1473661 to your computer and use it in GitHub Desktop.
Stubbing with arguments - examples used in my "(More) Specific Stubbing with RSpec" blog post
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
specify "for large orders" do
sales_amount = 10000
credit_given = 100
look_back_period = 3
found_customer = Customer.new
Customer.stub(:has_large_purchases) \
# stub with arguments
.with(sales_amount, look_back_period) \
.and_return [found_customer]
GivesCreditToPreferredCustomers \
.for_large_orders(sales_amount, credit_given)
found_customer.total_credit.should == credit_given
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment