RSpec测试自定义验证器

时间:2013-02-28 13:14:53

标签: ruby-on-rails ruby rspec validation rspec-rails

我的模型中有以下验证器:

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={}>

我做错了什么?我怎样才能实现目标? 任何帮助或建议将不胜感激。 感谢。

5 个答案:

答案 0 :(得分:13)

问题在于,您期望@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

答案 1 :(得分:2)

没有错误,因为您没有调用触发错误的事件。这通常在创建或保存记录时发生。您可能不想在测试中访问数据库,然后您可以使用这样的方法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?。 (至少在我看来...... :-))

答案 2 :(得分:2)

此解决方案适用于我(使用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

答案 3 :(得分:0)

我认为该记录未经过验证,因此验证没有运行且没有错误。您可以在代码输出中看到这一点。 &#34;验证:false&#34;

尝试:

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

答案 4 :(得分:0)

您实际上没有测试过验证,而且我建议您制作一个规格。

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