我的模型中有以下验证器:
class ContinuumValidator < ActiveModel::Validator
def validate(record)
if !record.end_time.nil? and record.end_time < record.start_time
record.errors[:base] << "An event can not be finished if it did not start yet..."
end
end
end
class Hrm::TimeEvent < ActiveRecord::Base
validates_with ContinuumValidator
end
如何使用Rspec进行测试?
这是我到目前为止所尝试的:(感谢zetetic)
describe "validation error" do
before do
@time_event = Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2)
end
it "should not be valid if end time is lower than start time" do
@time_event.should_not be_valid
end
it "raises an error if end time is lower than start time" do
@time_event.errors.should include("An event can not be finished if it did not start yet...")
end
end
但是我得到以下错误:
1) Hrm::TimeEvent validation error raises an error if end time is lower than start time
Failure/Error: @time_event.errors.should include("An event can not be finished if it did not start yet...")
expected #<ActiveModel::Errors:0x007fd1d8e02c50 @base=#<Hrm::TimeEvent id: nil, start_time: "2012-10-05 08:00:00", end_time: "2012-10-05 07:00:00", event_type: 2, employee_id: nil, created_at: nil, updated_at: nil, not_punched: false, validated: false, replace_id: nil>, @messages={}> to include "An event can not be finished if it did not start yet..."
Diff:
@@ -1,2 +1,5 @@
-["An event can not be finished if it did not start yet..."]
+#<ActiveModel::Errors:0x007fd1d8e02c50
+ @base=
+ #<Hrm::TimeEvent id: nil, start_time: "2012-10-05 08:00:00", end_time: "2012-10-05 07:00:00", event_type: 2, employee_id: nil, created_at: nil, updated_at: nil, not_punched: false, validated: false, replace_id: nil>,
+ @messages={}>
我做错了什么?我怎样才能实现我的目标?任何帮助或建议将不胜感激。谢谢。
问题是您期望@time_event.errors
表现得像字符串数组。它没有,它返回ActiveModel::Errors。正如其他人指出的,您还需要通过调用valid?
:
it "raises an error if end time is lower than start time" do
@time_event.valid?
@time_event.errors.full_messages.should include("An event can not be finished if it did not start yet...")
end
这个解决方案适用于我(使用Mongoid):
模型class OpLog
...
field :from_status, type: String
field :to_status, type: String
...
validate :states_must_differ
def states_must_differ
if self.from_status == self.to_status
errors.add(:from_status, "must differ from 'to_status'")
errors.add(:to_status, "must differ from 'from_status'")
end
end
...
end
测试:it 'is expected to have different states' do
expect { create(:oplog, from_status: 'created', to_status: 'created').to raise_error(Mongoid::Errors::Validations) }
end
所以在你的情况下,我会写一个这样的测试(如果使用ActiveRecord):
it 'raises an error if end time is lower than start time' do
expect { create(Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2)) }.to raise_error(ActiveRecord::Errors)
end
没有错误,因为您没有调用触发错误的事件。这通常在创建或保存记录时发生。你可能不想在测试中碰到数据库,然后你可以使用valid?
方法,像这样:
it "raises an error if end time is lower than start time" do
@time_event.valid?
@time_event.errors.should include("An event can not be finished if it did not start yet...")
end
我个人会把这两个测试放到一个既然有效?在第一种情况下被调用。
也有一个小问题:if record.end_time
优于if !record.end_time.nil?
。(至少在我看来....: -)
我认为记录没有验证,因此验证没有运行,没有错误。您可以在代码输出中看到这一点。"验证:假"
试题:
it "raises an error if end time is lower than start time" do
@time_event.valid?
@time_event.errors.should include("An event can not be finished if it did not start yet...")
end
您实际上还没有测试验证,另外我建议您做一个单独的规范。
describe "validation error" do
before { @time_event = Hrm::TimeEvent.new(start_time: "2012-10-05 10:00:00", end_time: "2012-10-05 09:00:00", event_type: 2) }
it "raises an error if end time is lower than start time" do
@time_event.valid?
@time_event.errors.should include("An event can not be finished if it did not start yet...")
end
end
class ContinuumValidator < ActiveModel::Validator
def validate(record)
if record.end_time and record.end_time < record.start_time
record.error.add_to_base << "An event can not be finished if it did not start yet..."
end
end
end