【发布时间】:2013-06-20 12:12:56
【问题描述】:
在我测试这个验证长度时遇到问题 shoulda_matchers ensure_length_of
型号
class Plant < ActiveRecord::Base
validates :code, :length => { :in => 2..6 }
end
Rspec
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)>'
谢谢!
【问题讨论】:
-
我认为消息应该用 // 而不是引号括起来。它必须是一个正则表达式。
-
@prasad.surase 我也尝试了正则表达式(/您的消息在这里/)。它没有工作
-
在没有任何
subject语句的情况下,it子句正在运行 Plant 的new实例,而不是 FactoryGirl 实例。这会给shoulda带来问题吗?此外,虽然我认为这不是一个因素,但文档建议您应该使用with_too_short_message而不是with_message。
标签: ruby ruby-on-rails-3 rspec shoulda