状态机,模型验证和RSpec [英] State Machine, Model Validations and RSpec
本文介绍了状态机,模型验证和RSpec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我当前的班级定义和规格:
Here's my current class definition and spec:
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
运行规范时,我遇到了两次失败,一次成功.我不知道为什么.对于其中两个状态,end_time_before_final
方法中的return if end_time.blank?
语句每次均应为false时,其计算结果为true. 推迟"是唯一似乎通过的状态.您对这里可能发生的事情有任何了解吗?
When I run the spec, I get two failures and one success. I have no idea why. For two of the states, the return if end_time.blank?
statement in the end_time_before_final
method evaluates to true when it should be false each time. 'postponed' is the only state that seems to pass. Any idea as to what might be happening here?
推荐答案
查看全文