Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails test validates_associated on model spec

Is there an equivalent to

  it { should validate...


  it { should validate_uniqueness_of(

etc for

 validates_associated

Something like,

it { should validate_associate(:associated_model_object)

except, you know, functional.

Thanks!

like image 351
Abraham P Avatar asked May 09 '26 16:05

Abraham P


1 Answers

I think something like this would work:

it "should validate associate_model_object" do
  m = Model.new(valid_params)
  m.associated_models.build(invalid_params)
  m.save.should eq(false)
end

Replace the middle line with this if you're coming from the other direction:

m.build_associated_model(invalid_params)
like image 70
Geoff Avatar answered May 12 '26 05:05

Geoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!