rspec是model.errors的语法

时间:2012-01-11 05:29:44

标签: ruby-on-rails-3 rspec

我收到以下代码的以下错误消息:

 Failure/Error: @league.errors.on(:short_name).should_equal "can't be blank"
 NoMethodError:
   undefined method `on' for #<ActiveModel::Errors:0x000001017853f0>


@league.errors.on(:short_name).should_equal "can't be blank"

根据我正在阅读的几个文档参考资料,这应该有效。知道发生了什么吗?

1 个答案:

答案 0 :(得分:0)

由于@location.errors是哈希:

require 'rspec'

describe "Location" do
  before(:each) do
    @location = double("Location")
    @location.stub(:errors).and_return(:short_name => "can't be blank")
  end

  it "should work like a hash" do
    @location.errors[:short_name].should == "can't be blank"
  end
end