When testing a model with a foreign key, I’d like to assert the model can’t be saved with an inexistent foreign key.
Example class for testing:
class Wheel
belongs_to: car
end
So, a unit test would look like this:
def test "a wheel must belong to an existent car"
@wheel = Wheel.new
@wheel.car_id = INEXISTENT_CAR_ID
assert !@wheel.save
end
What is the best way to find a valid INEXISTENT_CAR_ID (knowing fixtures are loaded with random ids)?
I like Chowlett’s approach. But it fails, if there is no record at all, so you might want to write