使用FactoryGirl创建对象时,ActiveRecord :: RecordNotSaved

时间:2014-12-03 13:24:34

标签: ruby-on-rails ruby unit-testing factory-bot

在FactoryGirl的帮助下编写单元测试时,我需要创建一个SysNav对象并将其存储在数据库中。调用构建工作正常,但当我告诉FactoryGirl创建该对象时,我得到一个ActiveRecord :: RecordNotSaved错误。

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'    

它似乎不是验证错误或由任何after_save方法引起。

我的工厂创建了这样的对象:

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

通过rails控制台创建具有这些值的对象非常正常。

我的单元测试以这种方式调用它:

    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

我正在使用Rails 3.2.8,factory_girl_rails 4.5.0,test-unit 3.0.7和mysql2 0.3.14。

2 个答案:

答案 0 :(得分:1)

就我而言,这是一个引发此错误的验证错误。 用于调试

    @record = FactoryGirl.create(:sys_nav)

如果上述情况出现错误,则记录为零。所以我们能做的是

    begin
      @record = FactoryGirl.build(:sys_nav)
      @record.save!
    rescue => e
      puts @record.errors.full_messages
    end

如果是因为验证错误,它会在这里返回错误。

答案 1 :(得分:0)

没有验证错误。 我的同事为lib \ lib_global添加了一个锁,用于除开发之外的所有环境。 在这里添加测试环境解决了错误。

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"