升级到RSpec 2:错误被替换为have(n).errors_on?需要解决方法

时间:2011-07-18 07:44:00

标签: ruby-on-rails-3 rspec

以下是我的rspec 1.x代码中的一些示例测试:

[:email, :contact_type_id].each do |attr|
  it "requires #{attr}" do
    e = EmailAddress.new
    e.should_not be_valid
    # i don't care how many errors there are,
    # just that there were errors for this attr.
    e.errors(attr).should_not be_nil
  end
end

RSpec 2.6.x强迫我这样做:

[:email, :contact_type_id].each do |attr|
  it "requires #{attr}" do
    e = EmailAddress.new
    e.should_not be_valid
    # have expects that I pass a number here :(
    e.should have(n).error_on(attr)
  end
end

我不在乎有多少错误,只是在尝试验证模型时出现了错误。如果我可以这样做的话会很酷:

e.should have.errors_on(attr)

有人有任何想法吗?

1 个答案:

答案 0 :(得分:4)

你可以试试这个:

e.should have_at_least(1).error_on(attr)