Rails Arel unit test error

倾然丶 夕夏残阳落幕 提交于 2019-12-13 21:12:50

问题


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: "user@example.com", 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?


回答1:


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.com/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a



来源:https://stackoverflow.com/questions/27671756/rails-arel-unit-test-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!