Ori Kremer
2011-Dec-06 16:10 UTC
Rails - validate inclusion fails on rake test:integration
I see different results when running ''rake test'' and when running ''rake test:integration''. When running all tests with ''rake test'' all tests pass. When running ''rake test:integration'' I have a failed test because my model fails to validate inclusion of an attribute before saving. My model looks something like: class Order < ActiveRecord::Base belongs_to :payment_type validates :payment_type, :inclusion => PaymentType.all ... end PaymentType is a lookup table, which I have a fixture for. When debugging the save method in the Order model I see that it fails the inclusion validation (again, only when running just the integration tests), but PaymentType.all.include?(payment_type) evaluates to ''true''. Any ideas why it fails when running only integration tests? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Dec-06 18:49 UTC
Re: Rails - validate inclusion fails on rake test:integration
On 6 Dec 2011, at 16:10, Ori Kremer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I see different results when running ''rake test'' and when running ''rake > test:integration''. > > When running all tests with ''rake test'' all tests pass. > When running ''rake test:integration'' I have a failed test because my > model fails to validate inclusion of an attribute before saving. > > My model looks something like: > > class Order < ActiveRecord::Base > belongs_to :payment_type > validates :payment_type, :inclusion => PaymentType.all > ... > end > > > PaymentType is a lookup table, which I have a fixture for. > > When debugging the save method in the Order model I see that it fails > the inclusion validation (again, only when running just the integration > tests), but > > PaymentType.all.include?(payment_type) > > evaluates to ''true''. > > Any ideas why it fails when running only integration tests? >The call to validates (and thus the evaluation of its argument) happens when your order class is loaded. If this happens before your fixtures are loaded then you''ll effectively be doing validates :blah, :inclusion => [] Why running rake test versus rake test:integration matters I don''t know - perhaps one has extra dependencies that force fixtures to be loaded earlier or later, but either way I''ve generally tried to avoid this sort of thing. I''m not sure what your current code gets you over just validating the presence of the payment_type Fred> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ori Kremer
2011-Dec-07 15:00 UTC
Re: Rails - validate inclusion fails on rake test:integration
Frederick Cheung wrote in post #1035416:> On 6 Dec 2011, at 16:10, Ori Kremer <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> belongs_to :payment_type >> >> PaymentType.all.include?(payment_type) >> >> evaluates to ''true''. >> >> Any ideas why it fails when running only integration tests? >> > > The call to validates (and thus the evaluation of its argument) happens > when your order class is loaded. If this happens before your fixtures > are loaded then you''ll effectively be doing validates :blah, :inclusion > => [] > > Why running rake test versus rake test:integration matters I don''t know > - perhaps one has extra dependencies that force fixtures to be loaded > earlier or later, but either way I''ve generally tried to avoid this sort > of thing. I''m not sure what your current code gets you over just > validating the presence of the payment_type > > FredThanks Fred. You''re right, when debugging the validate inclusion code, I see that the PaymentType.all returns []. I guess, like you said, that when the Order class is loaded and the inclusion is evaluated the PaymentType fixture wasn''t loaded yet. But that brings up another question, isn''t that a bug in rake/rails? Why aren''t the fixtures being loaded prior to being used? Why isn''t PaymentType loaded before Order is? Ori -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Dec-07 15:31 UTC
Re: Rails - validate inclusion fails on rake test:integration
On Dec 7, 3:00 pm, Ori Kremer <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks Fred. > You''re right, when debugging the validate inclusion code, I see that the > PaymentType.all returns []. > > I guess, like you said, that when the Order class is loaded and the > inclusion is evaluated the PaymentType fixture wasn''t loaded yet. > > But that brings up another question, isn''t that a bug in rake/rails? > > Why aren''t the fixtures being loaded prior to being used? Why isn''t > PaymentType loaded before Order is? >I don''t think any specific guarantees are made about when fixtures are loaded apart from the fact that by the time your test runs they are loaded or about the order in which any of this stuff happens. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ori Kremer
2011-Dec-08 14:15 UTC
Re: Rails - validate inclusion fails on rake test:integration
Frederick Cheung wrote in post #1035592:> On Dec 7, 3:00pm, Ori Kremer <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> PaymentType loaded before Order is? >> > I don''t think any specific guarantees are made about when fixtures are > loaded apart from the fact that by the time your test runs they are > loaded or about the order in which any of this stuff happens. > > FredI understand that fixtures are guaranteed to be fully loaded only when my test runs, the problem is, like you said, that the model class validation is evaluated before that. Another approach I tried was not to use fixtures at all, since payment types are actually seed data and not sample data. That approach failed as well for the same reason that the model validation is called from the engine initialization which is done before the seed data gets populated. The question remains, how can I use validates inclusion on a lookup table and not fail the test (when running only integration tests). Ori -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Dec-08 15:11 UTC
Re: Rails - validate inclusion fails on rake test:integration
On Dec 8, 2:15 pm, Ori Kremer <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I understand that fixtures are guaranteed to be fully loaded only when > my test runs, the problem is, like you said, that the model class > validation is evaluated before that. > > Another approach I tried was not to use fixtures at all, since payment > types are actually seed data and not sample data. > That approach failed as well for the same reason that the model > validation is called from the engine initialization which is done before > the seed data gets populated. > > The question remains, how can I use validates inclusion on a lookup > table and not fail the test (when running only integration tests). >why validate the inclusion at all ? I''d validate the presence of the foo_id column and use a foreign key constraint to ensure that it can''t contain junk. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ori Kremer
2011-Dec-09 03:43 UTC
Re: Rails - validate inclusion fails on rake test:integration
Frederick Cheung wrote in post #1035726:> On Dec 8, 2:15pm, Ori Kremer <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > why validate the inclusion at all ? I''d validate the presence of the > foo_id column and use a foreign key constraint to ensure that it can''t > contain junk. > > FredYou''re right, there''s no need to validate with inclusion as the foreign key constraint is enough. Works great. Thanks, Ori -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.