Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rspec - how to check model for validation that has ", if: :direct" clause

I have this model code:

validates_presence_of :setting_id  
validates_presence_of :user_id  
validates_presence_of :topic_id, if: :direct?

I have these rspec tests:

it { should validate_presence_of :setting_id }
it { should validate_presence_of :user_id }
it { should validate_presence_of(:topic_id).if(:direct?) } 

The first 2 tests work ok. The last one gives an error of:
undefined method 'if' for #<Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher:0x007fda6a5c6268>

How can I add a similar tests for code fragment for the last presence of validation for (:topic_id).if(:direct?)

like image 527
Michael Durrant Avatar asked Nov 22 '25 06:11

Michael Durrant


1 Answers

From the source code for shoulda-matchers, it looks like the :if condition is not supported. You will need to test this manually with stubbed values for the direct? method.

like image 144
Tanzeeb Khalili Avatar answered Nov 24 '25 20:11

Tanzeeb Khalili



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!