Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to mock and stub active record before_create callback with factory_girl
    primarykey
    data
    text
    <p>I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call back uses a 3rd party API to make a remote connection. I am using factory girl and would like to stub out this api so that when new factories are built during testing the remote calls are not made. </p> <p>I am using Rspec for mocks and stubs. The problem i'm having is that the Rspec methods are not available within my factories.rb</p> <p>model:</p> <pre><code>class PricePackage &lt; 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 =&gt; self.price_in_cents, :interval =&gt; 'month', :name =&gt; "#{::Rails.env} Item #{self.title}", :currency =&gt; 'usd', :id =&gt; self.title) rescue Exception =&gt; 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 </code></pre> <p>factory:</p> <pre><code>#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 </code></pre> <p>I'm not sure how to get the rspec mock and stub helpers loaded into my factories.rb and this might not be the best way to handle this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload