私は ActiveRecord モデル、PricePackage を持っています。これには before_create コールバックがあります。このコールバックは、サードパーティ API を使用してリモート接続を確立します。私は factory girl を使用しており、テスト中に新しいファクトリが構築されたときにリモート呼び出しが行われないように、この API をスタブ化したいと考えています。
モックとスタブに Rspec を使用しています。私が抱えている問題は、私のfactory.rb内でRspecメソッドが利用できないことです.
モデル:
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 モックとスタブ ヘルパーを factory.rb にロードする方法がわかりません。これは、これを処理する最善の方法ではない可能性があります。