0

当我测试这个时遇到一个问题,用 shoulda_matchers ensure_length_of验证长度

模型

class Plant < ActiveRecord::Base
  validates :code, :length => { :in => 2..6 } 
end

规格

require 'spec_helper'
describe Plant do

 before { @plant = FactoryGirl.create(:plant) } 
  it { should ensure_length_of(:code).is_at_least(2).with_message("is too short (minimum is 2 characters)")}
end

错误:

Failures:

  1) Plant
     Failure/Error: it { should ensure_length_of(:code).is_at_least(2).with_message("is too short (minimum is 2 characters)")}
       Did not expect errors to include "is too short (minimum is 2 characters)" when naics_code is set to "xx", got error: is too short (minimum is 2 characters)
     # ./spec/models/plant_spec.rb:11:in `block (2 levels) in <top (required)>'

谢谢!

4

1 回答 1

0

正如@Peter Alfvin 所提到的,问题出在问题上。Rspec 实现应如下所示:

require 'spec_helper'
describe Plant do

 before { @plant = FactoryGirl.create(:plant) }
  subject { @plant } 
  it { should ensure_length_of(:code).is_at_least(2).with_message("is too short (minimum is 2 characters)")}
end
于 2013-07-16T00:58:21.020 回答