Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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 on rails rspec失败:预期出现1个错误,得到3个错误 class-Card'TradeType',:foreign\u-key=>'trade\u-type\u-id' 属于:餐厅,:class\u name=>'restaurant',:foreign\u key=>'restaurant\u id' 验证是否存在:卡号,:message=>'卡号不能为空' 验证:卡片的\u编号,:message=>'卡号必须是数字' 验证以下内容的长度:卡号::is=>16,:message=>卡号必须是16位' 验证以下项的唯一性:卡号:message=>'卡号不能重复' 结束 描述卡片做什么 它“与卡号、姓名”一起有效 card=card.new( 卡号:“1054321239876456”, 姓名:"张三",, ) 期望(卡片)有效 结束 没有信用卡是无效的 卡=新卡(卡号:无) 期望(卡片).上有(1).个错误(:卡片号) 结束 结束_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails rspec失败:预期出现1个错误,得到3个错误 class-Card'TradeType',:foreign\u-key=>'trade\u-type\u-id' 属于:餐厅,:class\u name=>'restaurant',:foreign\u key=>'restaurant\u id' 验证是否存在:卡号,:message=>'卡号不能为空' 验证:卡片的\u编号,:message=>'卡号必须是数字' 验证以下内容的长度:卡号::is=>16,:message=>卡号必须是16位' 验证以下项的唯一性:卡号:message=>'卡号不能重复' 结束 描述卡片做什么 它“与卡号、姓名”一起有效 card=card.new( 卡号:“1054321239876456”, 姓名:"张三",, ) 期望(卡片)有效 结束 没有信用卡是无效的 卡=新卡(卡号:无) 期望(卡片).上有(1).个错误(:卡片号) 结束 结束

Ruby on rails rspec失败:预期出现1个错误,得到3个错误 class-Card'TradeType',:foreign\u-key=>'trade\u-type\u-id' 属于:餐厅,:class\u name=>'restaurant',:foreign\u key=>'restaurant\u id' 验证是否存在:卡号,:message=>'卡号不能为空' 验证:卡片的\u编号,:message=>'卡号必须是数字' 验证以下内容的长度:卡号::is=>16,:message=>卡号必须是16位' 验证以下项的唯一性:卡号:message=>'卡号不能重复' 结束 描述卡片做什么 它“与卡号、姓名”一起有效 card=card.new( 卡号:“1054321239876456”, 姓名:"张三",, ) 期望(卡片)有效 结束 没有信用卡是无效的 卡=新卡(卡号:无) 期望(卡片).上有(1).个错误(:卡片号) 结束 结束,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,您看到3个错误而不是1个错误的原因是卡。卡号为空。这意味着它也不是数字,也不是16个字符长 i、 e.所有三个 class Card < ActiveRecord::Base belongs_to :trade_type,:class_name => 'TradeType',:foreign_key => 'trade_type_id' belongs_to :restaurant,:class_name => 'Restaurant',:foreign_key

您看到3个错误而不是1个错误的原因是
卡。卡号为空。这意味着它也不是数字,也不是16个字符长

i、 e.所有三个

class Card < ActiveRecord::Base
  belongs_to :trade_type,:class_name => 'TradeType',:foreign_key => 'trade_type_id'
  belongs_to :restaurant,:class_name => 'Restaurant',:foreign_key => 'restaurant_id'
  validates_presence_of :card_no,:message => '卡号不能为空'
  validates_numericality_of :card_no,:message => '卡号必须是数字'
  validates_length_of :card_no,:is => 16,:message => '卡号必须是16位'
  validates_uniqueness_of :card_no,:message => '卡号不能重复'
end

describe Card do
  it 'is valid with card_no,name'do
     card = Card.new(
         card_no: '1054321239876456',
         name: 'zhangsan',
     )
    expect(card).to be_valid
  end

  it 'is invalid without a card_no' do
    card = Card.new(card_no:nil)
    expect(card).to have(1).errors_on(:card_no)
  end
end
我们正在失败

如果您希望仅在存在卡号时验证的数字性和长度,\u,请添加
allow\u blank:true


我们需要更多的信息,比如这个正在测试的类的代码。请注意thx。太好了!谢谢!我还想知道如何测试卡号的数字性,卡号的长度是16,有什么方法可以使用吗?你可以用同样的方法,只需更改
卡号的值即可。i、 e.
card=card.new(card\u no:'abc')
将测试非数字,
card=card.new(card\u no:'123456789012345')
将测试长度不超过16等。此外,您还可以访问
card.errors[:card\u no]
以检查您是否得到了故障的正确错误,而不仅仅是错误计数。没有问题。如果解决了您的问题,请接受答案;)我很感激你的帮助。
validates_presence_of :card_no,:message => '卡号不能为空'
validates_numericality_of :card_no,:message => '卡号必须是数字'
validates_length_of :card_no,:is => 16,:message => '卡号必须是16位'
validates_presence_of :card_no,:message => '卡号不能为空'
validates_numericality_of :card_no, :allow_blank => true, :message => '卡号必须是数字'
validates_length_of :card_no, :is => 16, :allow_blank => true, :message => '卡号必须是16位'