# Testing the use of form\_for to clone an existing record

**URL:** <https://www.ruby-forum.com/t/testing-the-use-of-form-for-to-clone-an-existing-record/174421>\
**Category:** Rails\
**Created:** [August 26, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/testing-the-use-of-form-for-to-clone-an-existing-record/174421 "2009-08-26T17:37:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [August 26, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/testing-the-use-of-form-for-to-clone-an-existing-record/174421/1 "2009-08-26T17:37:26Z")

</div>

I wanted to be able to clone fields from one record into a new record,  
so I added a select box to allow the user to select the record from  
which (s)he wanted to clone (using AJAX to dynamically update the view  
when (s)he selects such a record). In my #new action, I check to see  
if the :clone\_from parameter points to a valid record and if it does,  
I set my @record parameter to that record (created via  
old\_record.clone). If it doesn’t point to a valid record, I set  
@record to Record.new.

Here’s my problem…

in my view, I have

form\_for record do |f|  
…  
end

which generates a RESTful call to my #create action when @record was  
empty (i.e. filled in from Record.new), but generates a RESTful call  
to my #update action when @record is a clone of the old record.

Ahhhh… this is why this mailing list is so helpful… I start to  
write the email, I get to the part where I write “is a clone of the  
old record”, and I think to myself, “Wait a minute! Did I really call  
#clone when I went down that path?”… checked the code… found out  
that I didn’t… called #clone… problem solved.

Gee, thanks folks!!! You’re the greatest!!! (especially those of you  
who kept reading this far :-))

OK, now I’ve updated the subject line to reflect my (new) real  
question…  
When I first discovered this behavior, I thought to myself, “I should  
write a test case that demonstrates this bug, fix the bug, and show  
that the test case now passes”.

When I tried to write the functional test that demonstrated this  
(issue a #get call to the :new action with the :clone\_from parameter  
set, issue a #post :create call with the modified clone), the test  
worked perfectly. I had to look at the logs or HTML source to see  
that I was calling #update instead of #create.

How could I have tested this? Is it even worth writing a test case to  
demonstrate the incorrect operation of code that simply had a typo in  
it?

What do you think?

–wpd
