This simple example uses the DataMapper callback before :save(aka hook) to increment callback_count. callback_count is initialized to 0 and must be set to 1 by a callback.
This callback is called when a TestObject is created using:
TestObject.create()
but the callback is skipped when creating FactoryGirl via:
FactoryGirl.create(:test_object)
Any idea why? [Note: I am running ruby 1.9.3, factory_girl 4.2.0, data_mapper 1.2.0]
Full information...
DataMapper Model
class TestModel
include DataMapper::Resource
property :id, Serial
property :callback_count, Integer, :default => 0
before :save do
self.callback_count += 1
end
end
FactoryGirl Announcement
FactoryGirl.define do
factory :test_model do
end
end
RSpec test
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
Test results
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 ==)
Finished in 0.00534 seconds
2 examples, 1 failure
source
share