Vell
2013-Aug-22 15:24 UTC
How do I deal with ActiveRecord::RecordInvalid: Validation failed:
Hello all, I am currently writing model tests in rails 4. I am attempting to add an error to a reservation object if the total number of reservations for a given date and time has reached a pre-determined limit. When my test runs, it is hitting the appropriate code but it is raising the following error as opposed to just giving me an error in the object that I can use. ActiveRecord::RecordInvalid: Validation failed: Reservation limit reached for this time slot test/models/reservation_test.rb:29:in `block (2 levels) in <class:ReservationTest>'' I was wondering if anyone can give me an ideas of how to deal with this. My test environment is using shoulda, faker, and factory_girl test/models/reservation_test.rb: setup do List.create(name: ''reservation_limit'', values: 20) @reservation = FactoryGirl.build(:reservation) end should "remove unavailable times if reservation limit for time slot is full" do 20.times{ FactoryGirl.create :reservation } assert_equal false, FactoryGirl.create(:reservation), "reservation was created after total reservation for time slot was reached." end app/models/reservation.rb def check_reservation_time errors.add(:base, "Can''t set reservation to a time before 5pm.") if parse_time(start_time) < parse_time("17:00") errors.add(:base, "Can''t set reservation to a time after 8pm.") if parse_time(start_time) > parse_time("20:00") errors.add(:base, "Reservation limit reached for this time slot") if reservation_limit_reached?(Reservation.where("start_date = ? and start_time = ?", self.start_date, self.start_time)) end def reservation_limit_reached?(reservations) reservations.size >= List.where(name: ''reservation_limit'').first.values.to_i end I am told I can use `rescue_from ActiveRecord::RecordInvalid` but my research is pointing me to that only working in the controller so I wasn''t sure how that would help me in a model test. Any ideas or help is appreciated. Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/69469da3-4b0e-4d02-b32c-3cad8462e172%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Vell
2013-Aug-22 17:19 UTC
Re: How do I deal with ActiveRecord::RecordInvalid: Validation failed:
Interesting. I solved the issue finding a post from a person with the same error while trying to test. Issue was that i was trying to create an invalid object before I could test it. So instead, I assigned a valid object using Factory girl to a local variable then tested that the save was false and this resolved my issue. On Thursday, August 22, 2013 11:24:15 AM UTC-4, Vell wrote:> > Hello all, > > I am currently writing model tests in rails 4. I am attempting to add an > error to a reservation object if the total number of reservations for a > given date and time has reached a pre-determined limit. > > When my test runs, it is hitting the appropriate code but it is raising > the following error as opposed to just giving me an error in the object > that I can use. > > ActiveRecord::RecordInvalid: Validation failed: Reservation limit reached > for this time slot > > test/models/reservation_test.rb:29:in `block (2 levels) in > <class:ReservationTest>'' > > I was wondering if anyone can give me an ideas of how to deal with this. > > My test environment is using shoulda, faker, and factory_girl > > test/models/reservation_test.rb: > > setup do > > List.create(name: ''reservation_limit'', values: 20) > > @reservation = FactoryGirl.build(:reservation) > > end > > > should "remove unavailable times if reservation limit for time slot is > full" do > > 20.times{ FactoryGirl.create :reservation } > > assert_equal false, FactoryGirl.create(:reservation), "reservation > was created after total reservation for time slot was reached." > > end > > > app/models/reservation.rb > > def check_reservation_time > > errors.add(:base, "Can''t set reservation to a time before 5pm.") if > parse_time(start_time) < parse_time("17:00") > > errors.add(:base, "Can''t set reservation to a time after 8pm.") if > parse_time(start_time) > parse_time("20:00") > > errors.add(:base, "Reservation limit reached for this time slot") if > reservation_limit_reached?(Reservation.where("start_date = ? and start_time > = ?", self.start_date, self.start_time)) > > end > > > def reservation_limit_reached?(reservations) > > reservations.size >= List.where(name: > ''reservation_limit'').first.values.to_i > > end > > > I am told I can use `rescue_from ActiveRecord::RecordInvalid` but my > research is pointing me to that only working in the controller so I wasn''t > sure how that would help me in a model test. > > Any ideas or help is appreciated. > > Thanks. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/793557e1-7d7c-4beb-b4bb-ed778dbbe0c2%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Reasonably Related Threads
- validates_uniqueness_of(*attr_names) w scope not working in unit test
- RSpec: controller POST create
- [Rails 3.2.5] Rails: unit test fixture_path : fixture_file_upload cannot find the file ...
- Trouble with rspec and FactoryGirl Sequence
- Validation Failed: Userkey has already been taken, Email has already been taken