简体   繁体   中英

How to use FactoryGirl with ActiveRecord transaction rails

I Use Factory Girl a lot to create fake data in my database. But whenever I create a new factory and if a error occurs I end up with lot of inconsistent data since it does not support transaction no rollback occurs For example (snapshot of console)

INSERT INTO `INVOICES` (`created_by`, `creation_date`, `fulfillment_branch_id`, `invoice_date`, `invoice_number`, `invoice_status`, `last_updated`, `last_updated_by`, `order_id`, `order_status`, `parent_order_id`) VALUES ('xx.xx@xx.net', '2015-07-16 11:27:00', '1035280', NULL, NULL, 'Active', '2015-07-16 11:27:00', 'xx.xx@xx.net', 24, 'Invoiced', NULL)

  SQL (0.8ms)  INSERT INTO `packages` (`created_at`, `created_by`, `shipment_id`, `size`, `status`, `updated_at`, `updated_by`, `value`, `weight`) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545)
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'shipment_id' cannot be null: INSERT INTO `packages` (`created_at`, `created_by`, `shipment_id`, `size`, `status`, `updated_at`, `updated_by`, `value`, `weight`) VALUES ('2015-07-16 11:27:00', 49, NULL, 'LBH', 'active', '2015-07-16 11:27:00', 49, 2448.0, 0.32950726052542545)
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/db-charmer-1.9.0/lib/db_charmer/rails3/abstract_adapter/connection_name.rb:14:in `instrument'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:234:in `exec_insert'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/relation.rb:66:in `insert'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/persistence.rb:367:in `create'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/timestamp.rb:58:in `create'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in `block in create'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in `_run__221280857__create__997484455__callbacks'
... 58 levels...
    from /home/infibeam/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/observer.rb:194:in `notify_observers'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/evaluation.rb:20:in `notify'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:13:in `block in result'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:in `tap'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy/create.rb:9:in `result'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory.rb:42:in `run'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/activesupport-3.2.13/lib/active_support/notifications.rb:125:in `instrument'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/factory_runner.rb:22:in `run'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/factory_girl-4.4.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
    from (irb):4
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /home/infibeam/.rvm/gems/ruby-1.9.3-p551@pipes/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from scripFactoryGirl.reload

Can anyone explain where I should monkey patch to ensure factory runs transactionally?

Edit

To clear my question I want to patch FactoryGirl module so that my FactoryGirl.create api gets wrapped in ActiveRecord transaction everytime I call it.

Thank you

The reason of this error it's that you set for package table

shipment_id: nil 

To solve this you should set some info for this. Example:

FactoryGirl.define do
  factory :example do
    shipment

    #your another code

where is shipment - is association for Shipment-model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM