1

このモジュールが含まれている場合にのみ使用されることを意図したバリデーターで、非常に奇妙な問題が発生しています。特定のテストを実行すると、期待どおりに検証が失敗します。ただし、エラーはRecordInvalid;ではありません。代わりに、ArgumentError: wrong number of arguments (0 for 1)これは、この場合はまったく意味がありません。

これが検証コードを含むミックスインです。

module AccountStateIntegrityMixin
  def self.included(base)
    base.class_eval do
      base.validate :account_state_integrity
    end
  end

  def account_state_integrity
    history = self.account_state_history_entries.order("start ASC").all
    return if history.blank?

    vald_methods = [
      :end_with_nil,
      :no_self_transitions,
      :no_overlaps_or_gaps
    ]

    vald_methods.each do |m|
      p "history = #{history.inspect}"
      if !self.send(m, history)
        return
      end
    end
  end

  def no_self_transitions(*args)
    p "no_self_transitions"
    p args
    history = args[0]
    # I want everything except the last element. I want to compare each
    # element with its successor
    history[0..-2].each_with_index do |entry, i|
      next_elem = history[i+1]
      if entry.account_state_id == next_elem.account_state_id
        self.errors.add(:account_state_history, "has a self-transition entries with " +
                        "IDs #{entry.id} and #{next_elem.id}")
        self.errors.add(:no_self_transitions, "violated")
        return false
      end
    end

    true
  end

  def end_with_nil(*args)  # ArgumentError was being thrown here
    p "end_with_nil"
    p args
    history = args[0]
    last = history.last  # with debugging statement, NoMethodError here

    if last.end != nil
      self.errors.add(:account_state_history, "is missing a nil ending. Offending " +
                      "entry has ID = #{last.id}")
      self.errors.add(:end_with_nil, "violated")
      return false
    end

    true
  end

  def no_overlaps_or_gaps(*args)
    p "no_overlaps_or_gaps"
    p args
    history = args[0]
    # Again, everything except the last element
    history[0..-2].each_with_index do |entry, i|
      next_elem = history[i+1]
      if next_elem.start != entry.end
        self.errors.add(:account_state_history, "has an overlap or gap between " +
                        "entries with IDs #{entry.id} and #{next_elem.id}")
        self.errors.add(:no_overlaps_or_gaps, "violated")
        return false
      end
    end

    true
  end
end

お気づきかもしれませんが、テスト中に特定の変数の状態をデバッグするのに役立ついくつかのprintステートメントを追加しました。また、メソッドのパラメーターを変更して可変数のパラメーターを取得し、実際にメソッドに送信されているものを確認できるようにしました。(注1:デバッグを容易にするためにこれらの変更を行った結果、エラーはに変更されましたMethodError: You have a nil object when you didn't expect it!。注2:メソッドシグネチャの変更は主にdef method(history) -> def method(*args)と設定で構成されますhistory = args[0])これらのステートメントの結果としての出力は次のとおりです。

"history = [#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >]"
"end_with_nil"
[[#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >]]
"history = [#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >]"
"no_self_transitions"
[[#<AccountStateHistoryEntry id: 1007, account_id: 684, ... ]]
"history = [#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >]"
"no_overlaps_or_gaps"
[[#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >]]
"history = [#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >, <AccountStateHistoryEntry id: 1008, account_id: 684, ... >]"
"end_with_nil"
[[#<AccountStateHistoryEntry id: 1007, account_id: 684, ... >, #<AccountStateHistoryEntry id: 1008, account_id: 684, ... >]]
"end_with_nil"
[]

メソッドの内容を考えると、で明らかに渡されている引数が突然欠落しているaccount_state_integrity理由はわかりません。end_with_nilaccount_state_integrity

このバグがすでに修正されたレガシーバグによって引き起こされている場合に備えて、これを明確にします。古いバージョンのRuby(1.8.7)とRails(2.3.14)を使用しています。近い将来、RubyとRailsのより新しいバージョンに移行する予定です。

編集:単体テストからのフルスタックトレース出力

A free-trial account An account with no state with an account state history entry set to end in the future should still be able to end the account state:
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.last
app/models/account_state_integrity_mixin.rb:51:in `end_with_nil'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:42:in `send'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:42:in `value'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:125:in `default_options'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:36:in `full_message'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:287:in `full_messages'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:287:in `map'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:287:in `full_messages'
/Users/Daniel/.rvm/gems/.../activesupport/lib/active_support/whiny_nil.rb:52:in `inject'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:286:in `each'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:286:in `inject'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:286:in `full_messages'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:13:in `initialize'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:1101:in `new'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/validations.rb:1101:in `save_without_dirty!'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/dirty.rb:87:in `save_without_transactions!'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/transactions.rb:200:in `save!'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/transactions.rb:182:in `transaction_without_always_new'
config/initializers/always_nest_transactions.rb:11:in `transaction'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/transactions.rb:200:in `save!'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
/Users/Daniel/.rvm/gems/.../activerecord/lib/active_record/transactions.rb:200:in `save!'
app/models/account_state_lib.rb:260:in `clear_capability_overrides!'
app/models/account_state_lib.rb:321:in `clear_capabilities_cache'
app/models/account_state_lib.rb:256:in `clear_capability_overrides!'
app/models/account_state_lib.rb:121:in `end_state!'
test/unit/account_state_lib_test.rb:173:in `__bind_1340223707_845108'
/Users/Daniel/.rvm/gems/.../thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `call'
/Users/Daniel/.rvm/gems/.../thoughtbot-shoulda-2.11.1/lib/shoulda/context.rb:382:in `test: A free-trial account An account with no state with an account state history entry set to end
in the future should still be able to end the account state. '
/Users/Daniel/.rvm/gems/.../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Users/Daniel/.rvm/gems/.../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run'
 (2.221 s) (0.000 s)
4

1 に答える 1

2

問題は、ActiveRecord::Errors.addが属性にエラーを追加するときに使用されることを意図していることです。

end_with_nil属性ではありません。代わりにActiveRecord::Errors.add_to_baseを使用する必要があります。

errors.add他の行も修正する必要があります。

何が起こるかというと、Shoulda(テストランナー)は、という名前の属性にエラーがあることに気づき、end_with_nil次のような失敗したテストのメッセージを生成しようとします。

"You have an error for end_with_nil: value was #{record.end_with_nil}, 
 when it should have been xyz".

..したがって、end_with_nil引数なしで呼び出され、すべてが失敗します。

于 2012-06-20T20:15:04.743 に答える