Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect all array elements to be of the same class

I want to check that an array contains only objects of a specific class, let's say Float.

A working example at the moment:

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

Is there a way to validate if the array_to_test contains only Float instances?

Sample non-working pseudocode:

it "tests array_to_test class of elements" do
  expect(array_to_test).to be_a(Array[Float])
end

Do not consider Ruby and Rspec version as a restriction.

like image 407
Chris Lontos Avatar asked Feb 19 '16 12:02

Chris Lontos


People also ask

How do you get all elements with the same class name?

The querySelectorAll() method returns all elements within the document having the same class. To get the first element that matches the specified selector, you can use the querySelector() method.

How do you check if all elements of an array are the same?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.


1 Answers

Try all:

expect(array_to_test).to all(be_a(Float))
like image 180
potashin Avatar answered Sep 28 '22 11:09

potashin