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 验证包含值时出现验证错误:Rails_Ruby_Ruby On Rails 3_Validation_Ruby On Rails 4_Testing - Fatal编程技术网

Ruby 验证包含值时出现验证错误:Rails

Ruby 验证包含值时出现验证错误:Rails,ruby,ruby-on-rails-3,validation,ruby-on-rails-4,testing,Ruby,Ruby On Rails 3,Validation,Ruby On Rails 4,Testing,我在测试模型验证中的包含时出错。这是我的model.rb class Resident < ActiveRecord::Base validates :room_number,presence: true,uniqueness: {case_sensitive: false} validates :roll_number,presence: true,uniqueness:{case_sensitive: false} validates :name, presence: tr

我在测试模型验证中的包含时出错。这是我的model.rb

class Resident < ActiveRecord::Base
  validates :room_number,presence: true,uniqueness: {case_sensitive: false}
  validates :roll_number,presence: true,uniqueness:{case_sensitive: false}
  validates :name, presence: true,length: { maximum: 50 }
  validates :hostel,presence: true,inclusion: {in:%w(a b c h pg j frc e g i),message: "%{value} is not a valid hostel"}
end
我得到了这个错误

     test_should_be_valid#ResidentTest (1455250549.01s)
        Failed assertion, no message given.
        test/models/resident_test.rb:9:in `block in <class:ResidentTest>'
8 tests, 8 assertions, 1 failures, 0 errors, 0 skips
测试应为有效测试#剩余测试(1455250549.01s)
断言失败,未给出消息。
测试/模型/驻车测试。rb:9:in‘block in’
8次测试,8次断言,1次失败,0次错误,0次跳过

如何处理它。

我认为您应该首先尝试在测试中创建一个带有有效或无效hostel属性的
Resident
实例。然后你应该检查一下你的期望

test "hostel must be valid" do
  resident = Resident.create(@valid_attributes.merge({:hostel => 'a'})
  assert resident.valid?
end

test "hostel must be invalid" do
  resident = Resident.create(@valid_attributes.merge({:hostel => 'z'})
  assert_not resident.valid?
end

注意:
%w(a b c h pg j frc e g i)
是字符串数组
if@resident.hotel==%w(a b c h pg j frc e g i)
无法工作,因为您(我认为)在数组中检查字符串。

我认为您应该首先尝试在测试中使用有效或无效的hotel属性创建
resident
的实例。然后你应该检查一下你的期望

test "hostel must be valid" do
  resident = Resident.create(@valid_attributes.merge({:hostel => 'a'})
  assert resident.valid?
end

test "hostel must be invalid" do
  resident = Resident.create(@valid_attributes.merge({:hostel => 'z'})
  assert_not resident.valid?
end

注意:
%w(a b c h pg j frc e g i)
是字符串数组
if@resident.hotel==%w(a b c h pg j frc e g i)
无法工作,因为您要根据数组检查字符串。

这里的合并功能是什么?检查If语句中的字符串数组进行断言是不对的,也许我们可以在If语句merge将新的哈希放入现有的@valid_attributes哈希中一个接一个地比较字符串。因此,它会添加新的键:值对或更改现有键的值:您的包含验证会检查单个属性是否具有数组的/one值,但在测试中,您会查找数组==string的相等性。结果是错误的。您可以勾选:
%w(a b c h pg j frc e g i)。包括?(resident.hotel)
测试“hotel必须有效”如果%w(a b c h pg j frc e g i)是否包括?(@resident.hotel)断言@resident.valid?else assert_不是@resident.valid?结束
仍然相同8个测试、8个断言、1个失败、0个错误、0个跳过只是为了澄清一下
model\u测试。rb
resident\u测试相同。rb
?这里的合并功能是什么?检查If语句中的字符串数组进行断言是不对的,也许我们可以在If语句merge将新的哈希放入现有的@valid_attributes哈希中一个接一个地比较字符串。因此,它会添加新的键:值对或更改现有键的值:您的包含验证会检查单个属性是否具有数组的/one值,但在测试中,您会查找数组==string的相等性。结果是错误的。您可以勾选:
%w(a b c h pg j frc e g i)。包括?(resident.hotel)
测试“hotel必须有效”如果%w(a b c h pg j frc e g i)是否包括?(@resident.hotel)断言@resident.valid?else assert_不是@resident.valid?结束
仍然相同8次测试,8次断言,1次失败,0次错误,0次跳过只是为了澄清一下
model\u测试。rb
resident\u测试相同。rb