【问题标题】:ActiveRecord::RecordNotSaved when creating object with FactoryGirl使用 FactoryGirl 创建对象时的 ActiveRecord::RecordNotSaved
【发布时间】:2015-02-01 02:01:05
【问题描述】:

在 FactoryGirl 的帮助下编写单元测试时,我需要创建一个 SysNav 对象并将其存储在数据库中。调用 build 工作得很好,但是当我告诉 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。

【问题讨论】:

  • 验证可能失败。
  • 我看不到任何东西。构建时所有验证都运行干净,唯一的其他验证是 before_save,它也适用于这个对象。
  • 通过SysNav.create(:name =&gt; 'TestSysNav', :sn_self_id =&gt; 1, :level =&gt; 4, :padding_left =&gt; 70, :active =&gt; true, :created_by =&gt; 'UnitTest') 直接创建对象效果很好。

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


【解决方案1】:

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

    @record = FactoryGirl.create(:sys_nav)

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

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

如果由于验证错误,它将返回错误。

【讨论】:

    【解决方案2】:

    这不是验证错误。 我的同事在 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" 
    

    【讨论】:

    • 有趣——我第一次看到这个错误。恭喜你找到它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多