Topic: Using 'update_attributes' for testing

We're all familiar with using a statement like: @user.update_attributes(params[:user]).
I'm hoping I can use the update_attribute(s?) command to Unit test like this;
Build a healthy record and save it. For each field that has some type of constraint write a line like;

assert_failure? @user.fieldname = User.update_attribute(params[:user].[:fieldname]

I'm hoping to find a 'generic' way to DRY up my Unit testing procedures.
Thank you,
Kathy

Re: Using 'update_attributes' for testing

Here is an untested method that will fail the test if the given field/value pairs are actually valid. I hope this is what you're looking for. At the very least, it should give you some ideas on how to create your own assert methods by utilizing existing ones.

def assert_attr_failures(original_record, invalid_attrs)
  invalid_attrs.each do |field, value|
    record = original_record.clone
    record.send(field + '=', value)
    assert !record.save, "#{record.class} record shouldn't save if #{field} = #{value}."
    assert record.errors.invalid?(field), "#{record.class} #{field} should be invalid when = #{value}"
  end
end

You call this method like the following:
assert_attr_failures @valid_user, :name => '!', :email => '@err', :email => 'wrong@.'

Vincent Woo Ruby on Rails Blog