Ho un modello ActiveRecord, PricePackage. Questo ha un richiamo before_create. Questa richiamata utilizza un'API di terze parti per stabilire una connessione remota. Sto usando una ragazza di fabbrica e vorrei eliminare questa API in modo che quando vengono create nuove fabbriche durante il test, le chiamate remote non vengano effettuate.Come eseguire il mock e lo stub record attivo before_create callback con factory_girl
Sto usando Rspec per mock e stub. Il problema che sto avendo è che i metodi RSpec non sono disponibili nel mio factories.rb
modello:
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
fabbrica:
#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
non sono sicuro di come ottenere il rspec mock e stub helper caricati nel mio factories.rb e questo potrebbe non essere il modo migliore per gestirlo.
per inciso, quando si assegna una taglia a una domanda che taglia sarà preso dal tuo reputazione indipendentemente dal fatto che si assegna quindi è una buona cosa da fare a f seguitela e assegnala ad una delle risposte che le persone danno. Senza farlo evapora semplicemente –
Does 'pp.stub (: register_with_3rdparty) {true}' in 'after_build' solleva errori? – lulalala