rspec 检查数组元素中的数字实例

rspec check array elements for instances of numbers

如何检查数组的元素是否只包含数字(整数)?

 describe "#draw" do
   it "returns an array" do
     expect(@lottery_tip.draw).to be_a_kind_of Array
   end
   it "has six elements" do
     expect(@lottery_tip.draw.count).to eq(6)
   end
   it "s elements are only numbers" do
     expect(@lottery_tip.draw).to ???
   end
 end

我的简单 LotteryTip Class 有效,但我想知道如何检查返回数组中元素的类型...

我不确定这是最好的还是至少是好的做法,但我在之前定义了一个自己的方法 :each do block:

def is_number?
  @tip.all? { |x| x.is_a? Integer }
end

所以我可以签入 it 块:

   it "s elements are only numbers" do
     expect(is_number?).to eq(true)
   end

您可以使用 all matcher.

expect(@tip.draw).to all(be_an(Integer))