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.
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.
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.
Try all
:
expect(array_to_test).to all(be_a(Float))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With