Rails Arel单元测试错误

时间:2014-12-27 21:48:51

标签: ruby-on-rails unit-testing arel

我有以下错误:

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'

来自以下测试: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

在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

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

1 个答案:

答案 0 :(得分:0)

事实证明这不是Arel错误 - 这是因为列sociable_id和sociable_type 没有在架构中定义。我的愚蠢错误。 Arel提出异常是因为外键丢失了。但是Arel在这里有一个改进的错误信息: https://github.com/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a