Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 测试使用shoulda匹配器的sure_length_验证长度_Ruby_Ruby On Rails 3_Rspec_Shoulda - Fatal编程技术网

Ruby 测试使用shoulda匹配器的sure_length_验证长度

Ruby 测试使用shoulda匹配器的sure_length_验证长度,ruby,ruby-on-rails-3,rspec,shoulda,Ruby,Ruby On Rails 3,Rspec,Shoulda,当我测试时遇到一个问题,这将验证shoulda_matchers的长度 型号 class Plant < ActiveRecord::Base validates :code, :length => { :in => 2..6 } end 错误: Failures: 1) Plant Failure/Error: it { should ensure_length_of(:code).is_at_least(2).with_message("is too

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

型号

class Plant < ActiveRecord::Base
  validates :code, :length => { :in => 2..6 } 
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)>'
故障:
1) 种植
失败/错误:它{应确保长度为(:code.)。至少为(2)。带有消息(“太短(至少为2个字符)”}
当naics_代码设置为“xx”时,不希望错误包括“太短(最小为2个字符)”,得到错误:太短(最小为2个字符)
#./spec/models/plant_spec.rb:11:in‘分块(2层)’in’

谢谢

正如@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

我认为信息应该用//而不是引号括起来。它必须是regex.@prasad.surase我也尝试了regex方法(/你的消息在这里/)。在没有任何
主题
语句的情况下,
It
子句操作的是工厂的
实例,而不是FactoryGirl实例。这会给
shoulda
的工作带来问题吗?此外,尽管我不认为这是一个因素,但文档建议您应该将
与\u too\u short\u message
一起使用,而不是将
与\u message一起使用。
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