Zustandsmaschine, Modellvalidierungen und RSpec

Hier ist meine aktuelle Klassendefinition und Spezifikation:

class Event < ActiveRecord::Base

  # ...

  state_machine :initial => :not_started do

    event :game_started do
      transition :not_started => :in_progress
    end

    event :game_ended do
      transition :in_progress => :final
    end

    event :game_postponed do
      transition [:not_started, :in_progress] => :postponed
    end

    state :not_started, :in_progress, :postponed do
      validate :end_time_before_final
    end
  end

  def end_time_before_final
    return if end_time.blank?
    errors.add :end_time, "must be nil until event is final" if end_time.present?
  end

end

describe Event do
  context 'not started, in progress or postponed' do
    describe '.end_time_before_final' do
      ['not_started', 'in_progress', 'postponed'].each do |state|
        it 'should not allow end_time to be present' do
          event = Event.new(state: state, end_time: Time.now.utc)
          event.valid?
          event.errors[:end_time].size.should == 1
          event.errors[:end_time].should == ['must be nil until event is final']
        end
      end
    end
  end
end

Wenn ich die Spezifikation durchführe, erhalte ich zwei Fehler und einen Erfolg. Ich habe keine Idee warum. Für zwei der Staaten ist diereturn if end_time.blank? Aussage in derend_time_before_final Die Methode wird mit true ausgewertet, wenn sie jedes Mal false sein soll. "Aufgeschoben" ist der einzige Staat, der zu bestehen scheint. Irgendeine Idee, was hier passieren könnte?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage