0

I set up a User AR model which has conditional validation which is pretty much identical to the Railscast episode on conditional validation. So basically my User model looks like this:

class User < ActiveRecord::Base attr_accessor :password, :updating_password validates :password, :presence => true, :confirmation => true, :length => { :within => 6..40 }, :if => :should_validate_password? def should_validate_password? updating_password || new_record? end end 

Now in my action where the User can change their password I have the following two lines:

@user.updating_password = true if @user.update_attributes(params[:user]) ... 

so that I flag the validations to be run on the password. In development mode this works great - if the user tries to put in a password that is too short or too long the model does not pass validation. My problem is that for the life of me I can not get my tests for this to pass. Here is my spec:

require 'spec_helper' describe PasswordsController do render_views before(:each) do @user = Factory(:user) end describe "PUT 'update'" do describe "validations" do before(:each) do test_sign_in(@user) end it "should reject short passwords" do short = "short" old_password = @user.password @attr2 = { :password => short, :password_confirmation => short } put :update, :user_id => @user, :old_password => @user.password, :user => @attr2 @user.password.should == old_password end it "should reject long passwords" do long = "a" * 41 old_password = @user.password @attr2 = { :password => long, :password_confirmation => long } put :update, :user_id => @user, :old_password => @user.password, :user => @attr2 @user.password.should == old_password end end end end 

When I run these tests I always get the error:

1) PasswordsController PUT 'update' validations should reject short passwords Failure/Error: @user.password.should == old_password2 expected: "foobar" got: "short" (using ==) 

and of course the error for the password being too long. But should'nt the password be validated as a result of me setting @user.updating_password = true before any save attempts in the controller?

1 Answer 1

1

I think the problem isn't the code but what you expect it to do. When you call update_attributes and pass in a bad value, the value is saved into the model object even though the validation fails; the bad value has not been pushed to the database.

I think this makes sense because when the validation fails normally you would show the form again with the error messages and the inputs populated with the bad values that were passed in. In a Rails app, those values usually come from the model object in question. If the bad values weren't saved to the model they would be lost and your form would indicate that the old 'good' values had failed validation.

Instead of performing this check:

@user.password.should == old_password 

Maybe try:

@user.errors[:password].should_not == nil 

or some other test that makes sense.

Sign up to request clarification or add additional context in comments.

2 Comments

ah thanks this is really helpful. i guess my problem is though that the ultimate goal of this test is to ensure that the model in the database is unchanged. calling @user.reload before testing the condition and messing around with lambda do blocks dont seem to have an effect though - are you saying that there is no real way to test for this?
scratch that i realize what my problem is now. password is not a field in my database so reload of course would not reload that. i needed to compare the encrypted_password before and after to get this to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.