0

単体テストを実行すると、一部のテストで次のエラーが発生します。

     6) Error:
test_name_can't_be_nil(NodeTest):
NoMethodError: undefined method `detect' for :"Node#children":Symbol
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/naming.rb:85:in `model_name'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:315:in `generate_message'
    org/jruby/RubyArray.java:2339:in `collect'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:314:in `generate_message'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:235:in `add'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:256:in `add_on_blank'
    org/jruby/RubyArray.java:1615:in `each'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/errors.rb:254:in `add_on_blank'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations/presence.rb:9:in `validate'
    org/jruby/RubyBasicObject.java:1704:in `__send__'
    org/jruby/RubyKernel.java:2101:in `send'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:306:in `_callback_before_47'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:410:in `_run_validate_callbacks'
    org/jruby/RubyBasicObject.java:1698:in `__send__'
    org/jruby/RubyKernel.java:2097:in `send'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:212:in `run_validations!'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activemodel-3.1.4/lib/active_model/validations.rb:179:in `valid?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/validations.rb:29:in `valid?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/activesupport-3.1.4/lib/active_support/callbacks.rb:405:in `_run_validation_callbacks'
    /Users/dave/.rvm/gems/jruby-1.6.7.2/gems/neo4j-2.0.1-java/lib/neo4j/rails/callbacks.rb:26:in `valid_with_callbacks?'
    /Users/dave/Projects/myapp/test/test_helper.rb:11:in `assert_presence'
    /Users/dave/Projects/myapp/test/unit/node_test.rb:9:in `test_name_can't_be_nil'
    org/jruby/RubyBasicObject.java:1698:in `__send__'

問題となっている問題のテストは次のとおりです。

class NodeTest < ActiveSupport::TestCase
    def assert_presence( model, field)
        model.send( (field.to_s + '='), nil )
        model.valid?
        assert_match /can not be blank/, model.errors[field].join,
        "Presence error for #{field} not found on #model.class"
    end
    def setup
        @node = FactoryGirl.build :node
    end

    test "name can't be nil" do
        assert_presence @node, :name
    end
    ....
end

ノードはモデルで次のように定義されます。

class Node < Neo4j::Rails::Model
    include Neo4j::NodeMixin

    property :name, type: String, index: :exact
    property :description, type: String

    has_one(:creator).from(User, :created_nodes)

    has_n(:children).to(Node)              ## <--- Suspected difficult line
    has_n(:parents).from(Node, :children)  ## <--- Suspected difficult line
    has_n(:capable_users).from(User, :capabilities)

    # The following has the Class commented out because this somehow triggers
    # an error from the 'has_n(:children).to(Node)' line
    has_n(:challenges)  #.to(Challenge)
    ....
end

基本的に、サーバーは正常に動作しますが、valid を呼び出そうとするといつでもクラッシュしますか? ノード上 (他のモデルではありません)。なぜこれが起こっているのか誰にも分かりますか?

4

1 に答える 1

0

問題は、属性、つまり親(そしておそらく子供)の選択でした。

エラーが発生していたアクティブモデルのソース(ここ)を調べて、次のことを確認しました。

def model_name
  @_model_name ||= begin
    namespace = self.parents.detect { |n| n.respond_to?(:_railtie) }  # <--- this line
    ActiveModel::Name.new(self, namespace)
  end
end

ご覧のとおり、親が使用されているようです。名前を変更し、検証、保存などが機能するようになりました。

于 2012-07-03T14:25:46.277 に答える