我有一個ActiveRecord模型,PricePackage。這有一個before_create回調。此回調使用第三方API進行遠程連接。我正在使用工廠女孩,並希望將此API廢除,以便在測試過程中建立新工廠時不會進行遠程調用。如何模擬和存根活動記錄before_create與factory_girl回調
我使用Rspec模擬和存根。我遇到的問題是,Rspec的方法不可用我的factories.rb內
型號:
class PricePackage < ActiveRecord::Base
has_many :users
before_create :register_with_3rdparty
attr_accessible :price, :price_in_dollars, :price_in_cents, :title
def register_with_3rdparty
return true if self.price.nil?
begin
3rdPartyClass::Plan.create(
:amount => self.price_in_cents,
:interval => 'month',
:name => "#{::Rails.env} Item #{self.title}",
:currency => 'usd',
:id => self.title)
rescue Exception => ex
puts "stripe exception #{self.title} #{ex}, using existing price"
plan = 3rdPartyClass::Plan.retrieve(self.title)
self.price_in_cents = plan.amount
return true
end
end
廠:
#PricePackage
Factory.define :price_package do |f|
f.title "test_package"
f.price_in_cents "500"
f.max_domains "20"
f.max_users "4"
f.max_apps "10"
f.after_build do |pp|
#
#heres where would like to mock out the 3rd party response
#
3rd_party = mock()
3rd_party.stub!(:amount).price_in_cents
3rdPartyClass::Plan.stub!(:create).and_return(3rd_party)
end
end
我不知道怎麼弄的rspec模擬和存根助手加載到我的factories.rb中,這可能不是處理這個問題的最好方法。
順便說一句,當你將一個賞金給獎金將從您的名譽採取的一個問題,無論您指定所以這對f來說是件好事通過它並分配給人們給出的答案之一。沒有這樣做,它只是蒸發 –
'after_build'中的'pp.stub(:register_with_3rdparty){true}'會引發任何錯誤嗎? – lulalala