期望所有数组元素属于同一类

Chr*_*tos 5 ruby arrays testing rspec

我想检查一个数组是否只包含特定类的对象,比方说Float.

一个工作的例子:

it "tests array_to_test class of elements" do
  expect(array_to_test.count).to eq(2)
  expect(array_to_test[0]).to be_a(Float)
  expect(array_to_test[1]).to be_a(Float)
end
Run Code Online (Sandbox Code Playgroud)

有没有办法验证是否array_to_test只包含Float实例?

示例非工作伪代码:

it "tests array_to_test class of elements" do
  expect(array_to_test).to be_a(Array[Float])
end
Run Code Online (Sandbox Code Playgroud)

不要将Ruby和Rspec版本视为限制.

pot*_*hin 14

试试all:

expect(array_to_test).to all(be_a(Float))
Run Code Online (Sandbox Code Playgroud)