Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy is before :save callback hook not getting called from FactoryGirl.create()?
    primarykey
    data
    text
    <p>This simple example uses DataMapper's <code>before :save</code> callback (aka hook) to increment <code>callback_count</code>. callback_count is initialized to 0 and should be set to 1 by the callback.</p> <p>This callback is invoked when the TestObject is created via:</p> <pre><code>TestObject.create() </code></pre> <p>but the callback is skipped when created by FactoryGirl via:</p> <pre><code>FactoryGirl.create(:test_object) </code></pre> <p>Any idea why? [Note: I'm running ruby 1.9.3, factory_girl 4.2.0, data_mapper 1.2.0]</p> <p>Full details follow...</p> <h2>The DataMapper model</h2> <pre><code># file: models/test_model.rb class TestModel include DataMapper::Resource property :id, Serial property :callback_count, Integer, :default =&gt; 0 before :save do self.callback_count += 1 end end </code></pre> <h2>The FactoryGirl declaration</h2> <pre><code># file: spec/factories.rb FactoryGirl.define do factory :test_model do end end </code></pre> <h2>The RSpec tests</h2> <pre><code># file: spec/models/test_model_spec.rb require 'spec_helper' describe "TestModel Model" do it 'calls before :save using TestModel.create' do test_model = TestModel.create test_model.callback_count.should == 1 end it 'fails to call before :save using FactoryGirl.create' do test_model = FactoryGirl.create(:test_model) test_model.callback_count.should == 1 end end </code></pre> <h2>The test results</h2> <pre><code>Failures: 1) TestModel Model fails to call before :save using FactoryGirl.create Failure/Error: test_model.callback_count.should == 1 expected: 1 got: 0 (using ==) # ./spec/models/test_model_spec.rb:10:in `block (2 levels) in &lt;top (required)&gt;' Finished in 0.00534 seconds 2 examples, 1 failure </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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