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@googlegroups.com.
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.

Search Discussions

  • Frederick Cheung at Dec 6, 2011 at 6:52 pm

    On 6 Dec 2011, at 16:10, Ori Kremer 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@googlegroups.com.
    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@googlegroups.com.
    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 at Dec 7, 2011 at 3:01 pm

    Frederick Cheung wrote in post #1035416:
    On 6 Dec 2011, at 16:10, Ori Kremer 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

    Fred
    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?

    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@googlegroups.com.
    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.
  • Frederick Cheung at Dec 7, 2011 at 3:32 pm

    On Dec 7, 3:00 pm, Ori Kremer 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@googlegroups.com.
    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 at Dec 8, 2011 at 2:16 pm

    Frederick Cheung wrote in post #1035592:
    On Dec 7, 3:00pm, Ori Kremer 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.

    Fred
    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).

    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@googlegroups.com.
    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.
  • Frederick Cheung at Dec 8, 2011 at 3:12 pm

    On Dec 8, 2:15 pm, Ori Kremer 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@googlegroups.com.
    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 at Dec 9, 2011 at 3:43 am

    Frederick Cheung wrote in post #1035726:
    On Dec 8, 2:15pm, Ori Kremer 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.

    Fred
    You'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@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 6, '11 at 4:10p
activeDec 9, '11 at 3:43a
posts7
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Ori Kremer: 4 posts Frederick Cheung: 3 posts

People

Translate

site design / logo © 2021 Grokbase