Search code examples
ruby-on-railsrubyunit-testingfactory-bot

ActiveRecord::RecordNotSaved when creating object with FactoryGirl


While writing unit tests with help of FactoryGirl I need to create a SysNav object and store it in the database. Calling build works just fine, but when I tell FactoryGirl to create that object I get an ActiveRecord::RecordNotSaved Error.

ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/persistence.rb:104:in    `save!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `block in save!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:208:in `transaction'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `save!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/notifications.rb:125:in `instrument'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
C:/Projekte/Rails/trunk/test/unit/sys_nav_test.rb:23:in `test_create'
(eval):12:in `run'
C:/Program Files (x86)/JetBrains/RubyMine 5.4.3.2.1/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:93:in `start_mediator'
C:/Program Files (x86)/JetBrains/RubyMine 5.4.3.2.1/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:81:in `start'    

It does not seem to be an validation error or caused by any after_save method.

My factory creates the object like this:

      factory :sys_nav do
         name 'TestSysNav'
         sn_self_id 1
         level 4
         padding_left 70
         active true
         created_by 'UnitTest'
      end

Creating the object with these values via rails console works perfectly fine.

My Unit Test calls it this way:

    require 'test/unit'
    require 'test_helper'

    class SysNavTest < Test::Unit::TestCase

    # Called before every test method runs.
    def setup
    # Do nothing
    end

    # Called after every test method runs.
    def teardown
      sys_nav = SysNav.find_by_name 'TestSysNav'
      sys_nav.destroy if sys_nav
    end

    def test_build
      FactoryGirl.build(:sys_nav)
    end

    def test_create
      FactoryGirl.create(:sys_nav) # <-- Error occurs here
    end 

   end

I am working with Rails 3.2.8, factory_girl_rails 4.5.0, test-unit 3.0.7 and mysql2 0.3.14.


Solution

  • It was no validation error. My co-worker added a lock in lib\lib_global for all environments, except development. Adding the test environment here solved the error.

    LOCK_SYS_IDS = system_setting.lock_sys_id
        LOCK_SYS_IDS = false      if Rails.env == "development"
        LOCK_SYS_IDS = false      if Rails.env == "test"
    
    LOCK_SYS_TABLES = system_setting.lock_sys_tables
        LOCK_SYS_TABLES = false   if Rails.env == "development"          
        LOCK_SYS_TABLES = false   if Rails.env == "test"