Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing errors when using :delegate without :prefix on 3.0.x #2006

Closed
jan0sch opened this issue Jul 7, 2011 · 1 comment
Closed

Unit testing errors when using :delegate without :prefix on 3.0.x #2006

jan0sch opened this issue Jul 7, 2011 · 1 comment

Comments

@jan0sch
Copy link

jan0sch commented Jul 7, 2011

Hi,

I'm running into an issue using the delegate method on rails 3.0.9 without the prefix option.
Here are my two models:

    class User < ActiveRecord::Base
      acts_as_authentic

      has_one :user_condition

      validates_presence_of :username
      validates_presence_of :password, :if => :new_record?
      validates_presence_of :password_confirmation, :if => :new_record?

      delegate  :height,
                :weight,
                :resting_heart_rate,
                :blood_pressure_systolic,
                :blood_pressure_diastolic,
                :abdominal_girth,
                :to => :user_condition
    end

    class UserCondition < ActiveRecord::Base
      attr_accessible :user_id, :user, :height, :weight, :resting_heart_rate, :blood_pressure_systolic, :blood_pressure_diastolic, :abdominal_girth

      belongs_to :user

      validates_presence_of :user
      validates_uniqueness_of :user
      validates_numericality_of :height, :weight, :resting_heart_rate, :blood_pressure_systolic, :blood_pressure_diastolic ,:abdominal_girth
    end

This works as expected within the application. Meaning if I use @user.height in my view it works.
However if I do run the following unit test it fails:

    class UserConditionTest < ActiveSupport::TestCase
      def test_should_be_valid
        attributes = {}
        attributes[:user] = User.first
        attributes[:height] = 0
        attributes[:weight] = 0
        attributes[:blood_pressure_systolic] = 0
        attributes[:blood_pressure_diastolic] = 0
        attributes[:resting_heart_rate] = 0
        attributes[:abdominal_girth] = 0
        assert UserCondition.new(attributes).valid?
      end
    end

If I add the option

    :prefix => true

to the :delegate call the unit test runs fine. Although I have to change my views.

I tried to workaround the problem using something like

    delegate :height, :to => "self.try(:user_condition) or return ''"

but that did not help regarding the unit test. :-(

@jan0sch
Copy link
Author

jan0sch commented Jul 8, 2011

I just saw that the problem is probably not related to delegate as running "rake test" instead of "rake test:recent" also fails even if I use the prefix option. Closing this for now. I'll dig into with a little more time on my hands.

@jan0sch jan0sch closed this as completed Jul 8, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant