Rails在单元测试中“验证包含”奇怪的行为

时间:2012-09-28 16:30:55

标签: ruby-on-rails unit-testing validation

我有一个模型,Address,带有type属性和以下验证:

class Address < ActiveRecord::Base

  TYPES = {
    main: 1,
    warehouse: 2,
    invoicing: 3 
  }

  attr_accessible :type

  # validations
  validates :type, presence: true
  validates :type, :inclusion => { :in => Address::TYPES.values,:message => "%{value} is not a valid address type" }
end

事实是,当我在rails控制台中运行此验证时,我按预期工作:

1.9.3p194 :001 > a = Address.first
1.9.3p194 :007 > a.address_type = 4
=> 4 
1.9.3p194 :008 > a.valid?
=> false 

我已经创建了一个单元测试来测试它,如下所示:

test "address type should have a valid value" do
  address = Address.first
  address.address_type = 4
  assert address.invalid?, "address is valid with an invalid type value"
end

但测试正在执行,没有失败。 在测试中是否有我遗漏或做错的事情,所以当我在rails控制台上尝试它时它不起作用?

提前坦克你。

0 个答案:

没有答案