3

i am new to shoulda. many of my models have validations as below

validates :sampling_method, :presence => true, :if => :type_of_resource
validate :check_for_decimal_places, :if => :is_size_and_gdnt, :on => :update

here, type_of_resource and is_size_and_gdnt are instance methods for model

my shoulda validation for first is as

it { should validate_presence_of(:sampling_method) }

but i dont know how to add check for if

i have checked the second validation using by creating a object using factory girl, and checking its values when the object is saved but how do i test the second validation using shoulda

Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
  • Check out this old but likely still valid writeup http://blog.douglasfshearer.com/post/17547031679/testing-custom-rails-validations-with-shoulda Also see http://stackoverflow.com/questions/10994373/rspec-shoulda-testing-that-a-custom-validator-is-called – Abram Apr 08 '13 at 08:15
  • @Abram the first validation solution shouldnt be checked using 'allow_values' since its depends upon the result (true/false) provided by the method specified in the 'if' condition. if the calculations in the method are dependent on many other models then the validation is heavy or may be impossible using valid invalid values using 'allow_values'. i merely want to check if the method has been specified or not. – Prasad Surase Apr 11 '13 at 11:27
  • @Abram i think shoulda might be creating a object internally using ruby reflection and the apis for active_model to check if the specific validation has been added and the necessary conditions specified. but it doesnt consider all the possible conditions (here, 'if'). – Prasad Surase Apr 11 '13 at 11:29

0 Answers0