Rails Arel 单元测试错误

编程入门 行业动态 更新时间:2024-10-26 19:38:28
本文介绍了Rails Arel 单元测试错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有以下错误:

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: "user@example", 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/rails/rails/commit/78bd18a90992e3459d78bd18a90992e3459d767aI 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: "user@example", 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_type weren'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/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a

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

更多推荐

[db:关键词]

本文发布于:2023-04-28 18:09:59,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   错误   Rails   Arel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!