本文介绍了Rails Arel 单元测试错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下错误:

ERROR["test_character_should_be_valid", CharacterTest, 0.214787]test_character_should_be_valid#CharacterTest (0.21s)NoMethodError:NoMethodError:$1"的未定义方法`val':Arel::Nodes::BindParamapp/models/user.rb:11:in `block in '测试/模型/character_test.rb:8:在设置"中

来自以下测试:test/models/character_test.rb:

 需要 'test_helper'类 CharacterTest <ActiveSupport::TestCase定义设置@user = User.new(name: "Example User", email: "[email protected]", callsign: "example",密码:foobar",password_confirmation:foobar")@user.save@character = @user.character结尾测试字符应该有效"做断言@character.valid?结尾结束 # 字符测试

在 gemfile 中:

gem 'arel', '6.0.0.beta2'

character.rb:

class 字符 <ActiveRecord::Base归属地:社交,多态:真结尾

user.rb:

class User 

我找不到有关此特定 arel 错误的任何信息.我的 arel gem 是 6.0.0.beta2,它似乎解决了其他问题(如 此处).以前有人见过这样的吗?

解决方案

事实证明这不是 Arel 错误 - 这是因为列 sociable_id 和 sociable_type没有在架构中定义.我愚蠢的错误.由于缺少外键,Arel 引发了异常.但是,Arel 此处有改进的错误消息:https://github.com/rails/rails/commit/78bd18a90992e3459d78bd18a90992e3459d767a

I have the following error:

ERROR["test_character_should_be_valid", CharacterTest, 0.214787]
test_character_should_be_valid#CharacterTest (0.21s)
NoMethodError:         NoMethodError: undefined method `val' for "$1":Arel::Nodes::BindParam
app/models/user.rb:11:in `block in <class:User>'
test/models/character_test.rb:8:in `setup'

From the following test: test/models/character_test.rb:

    require 'test_helper'

    class CharacterTest < ActiveSupport::TestCase

      def setup
        @user = User.new(name: "Example User", email: "[email protected]", callsign: "example",
                         password: "foobar", password_confirmation: "foobar")
        @user.save
        @character = @user.character
      end

      test "character should be valid" do
        assert @character.valid?
      end

    end # CharacterTest

In the gemfile:

gem 'arel', '6.0.0.beta2'

character.rb:

class Character < ActiveRecord::Base
  belongs_to :sociable, polymorphic: true
end

user.rb:

class User < ActiveRecord::Base

  has_one  :character, as: :sociable

  before_validation do
    self.create_character unless character # line 11
  end
  .
  .
end

I can't find anything about this particular arel error. My arel gem is 6.0.0.beta2, which seems to fix other problems (as in here). Has anyone seen anything like this before?

解决方案

It turns out this isn't an Arel error - it's because the columns sociable_id and sociable_typeweren't defined in the schema. My stupid mistake. Arel was raising an exception because the foreign key was missing. However there is an improved error message at Arel here:https://github.com/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a

这篇关于Rails Arel 单元测试错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:45