# Virtual attributes and model validation (unit testing)

**URL:** <https://www.ruby-forum.com/t/virtual-attributes-and-model-validation-unit-testing/65674>\
**Category:** Rails\
**Created:** [August 20, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/virtual-attributes-and-model-validation-unit-testing/65674 "2006-08-20T13:14:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/virtual-attributes-and-model-validation-unit-testing/65674/1 "2006-08-20T13:14:13Z")

</div>

I’m using a virtual attribute for password in my user model and a  
hashed\_password field for the persisted hashed value (a la AWDWR).

However, I am having issues using fixtures and unit tests to validate my  
models.

Example, I have a User model that has a password validation clause that  
looks something like this:

validates\_length\_of :password, :within =\> 6…30

And I have a fixture for a test user that looks something like this:

user\_jeff:  
id: 1  
username: jeff  
real\_name: Jeff Shmeff  
salt: \<%= SALT %\>  
hashed\_password: \<%= User.encrypted\_password(‘secret’, SALT) %\>  
email: removed\_email\_address@domain.invalid

if I ever attempt something like this in my User unit tests…

user = users(:user\_jeff)  
assert user.valid?

I will get an assertion failure like this:  
‘Password is too short (minimum is 6 characters)’

temporarily to get around this, I’ve taken to doing something like this  
in my test setup method:

def setup  
@user = User.find(1)  
@user.password = “this is a hack”  
end

and usint @user for most of my tests instead of users(:user\_jeff).  
But, that sucks.

Any suggestions as to what I SHOULD be doing?

Jeff
