# Test\_create gets 200 instead of :success

**URL:** <https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089>\
**Category:** Rails\
**Created:** [October 21, 2006, 4:43am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089 "2006-10-21T04:43:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 21, 2006, 4:43am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/1 "2006-10-21T04:43:50Z")

</div>

Help!

I have a new/create process that works fine in the actual app. But when  
I test I always get a failure in the test\_create section. Here’s some  
controller code:

def new  
end

def create  
begin  
@borrower = Borrower.new(params[:borrower])  
if @borrower.save  
flash[:notice] = ‘Borrower ’ + @borrower.FirstName + ’ ’ +  
@borrower.LastName + ’ was added.’  
redirect\_to :action =\> ‘list’  
else  
flash[:notice] = ‘Borrower could not be added. Must have a Last  
Name and a unique SSN.’  
render :action =\> ‘new’  
end  
rescue  
flash[:notice] = ‘Borrower could not be added.’  
render :action =\> ‘new’  
end  
end

Setting breakpoints at different places in the above code indicates that  
my test is going through the rescue section.

Here’s the test code:  
def test\_new  
get :new  
assert\_response :success  
assert\_template ‘new’  
end  
def test\_create  
num\_borrowers = Borrower.count  
post :create, :borrower =\> {:SSN =\> ‘000’, :LastName =\> ‘Anyone’}  
assert\_response :redirect # gets 200 (OK) instead of 302  
(Found)  
assert\_redirected\_to :action =\> ‘list’  
assert\_equal ‘Borrower Anyone was added.’, flash[:notice]  
assert\_equal num\_borrowers + 1, Borrower.count  
end

What am I leaving out of my test to keep me from :success? Or should it  
be rearranged somehow? As I said, the app itself adds a borrower  
correctly.

TIA,  
Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 21, 2006, 5:15am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/2 "2006-10-21T05:15:50Z")

</div>

Get freelance or telecom-job here!!

> **[Google Sites: Sign-in](https://accounts.google.com/v3/signin/identifier?dsh=S1225198800%3A1689802857190158&continue=https%3A%2F%2Fsites.google.com%2Fsite%2Fsites%2Fsystem%2Ferrors%2FWebspaceNotFound%3Fpath%3D%2Ffreelancetelecom%2F&followup=https%3A%2F%2Fsites.google.com%2Fsite%2Fsites%2Fsystem%2Ferrors%2FWebspaceNotFound%3Fpath%3D%2Ffreelancetelecom%2F&ifkv=AeDOFXiuKIpPqHVm4qdGvKwKMgwtezVjj9vN16gl5eVxHJqA7asoyHXhPbJRTvF3E_kSHLNXFeRv&passive=1209600&service=jotspot&flowName=GlifWebSignIn&flowEntry=ServiceLogin)**
>
> Access Google Sites with a personal Google account or Google Workspace account (for business use).

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 22, 2006, 2:07am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/3 "2006-10-22T02:07:18Z")

</div>

Hi Shauna, I would recommend doing the following for your test\_create:

def test\_create

# obtain the number of borrowers.

num\_borrowers = Borrower.count

# post the data to the server

post :create, :borrower =\> {:SSN =\> ‘000’, :LastName =\> ‘Anyone’}

# was the response successful.

assert\_response :success

# did we get redirected to the correct page.

assert\_redirected\_to :action =\> ‘list’

# did the appropriate message get displayed on success.

assert\_equal ‘Borrower Anyone was added.’, flash[:notice]

# did the number of borrowers get incremented.

assert\_equal num\_borrowers + 1, Borrower.count

end

I’m new to RoR and I have also completed the ‘Ruby on Rails’ course  
recently in Dallas.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 23, 2006, 9:00am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/4 "2006-10-23T09:00:58Z")

</div>

Hi…I’m pretty new to rails as well, but maybe I can help.

The response code will be either 200 (success) or 302 (redirect). Not  
both.

So, you’re orignal test code looks good to me.

Unfortunatley, I don’t know what the problem is though.

Or maybe try adding some code to see if the Borrower was created  
properly, before saving, just to doublecheck that the parameters were  
passed in properly.

@borrower = Borrower.new(params[:borrower])  
puts “Borrower SSN: #{@borrower.LastName}” #  
doublecheck the new borrowers LastName  
if @borrower.save

Is it possible that “anyone” is already in the db?

Good Luck!

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 23, 2006, 8:09am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/5 "2006-10-23T08:09:56Z")

</div>

Thanks for responding, Conrad. I substituted the line  
assert\_response :success  
as you suggested, and now the Failure message reads  
Expected response to be a \<:redirect\>, but was \<200\>

In other words, the :success test passes but Rails still chokes on the  
following line.

Does anyone know why the test goes through the rescue section while my  
app itself does not?

I’ve been tearing my hair out for days over this one!

Thanks,  
Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 23, 2006, 9:58am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/6 "2006-10-23T09:58:19Z")

</div>

Sorry, I meant to say, if I add the (duplicate) line  
@borrower = Borrower.new(params[:borrower])  
to def new, the test doesn’t fail and the app still works fine.

Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 23, 2006, 9:55am UTC](https://www.ruby-forum.com/t/test-create-gets-200-instead-of-success/73089/7 "2006-10-23T09:55:59Z")

</div>

Hi Chris,

Thanks for the suggestion. Yes, the @borrower.LastName comes out  
correctly in both the test and the real app.

So I’m back to –

WHY does the test go through the rescue section while the app itself  
executes correctly?

Can I get the attention of anyone who knows what’s going on here? I’ve  
used Google Code Search to see if anyone else does the test\_new …  
test\_create sequence this way. The only one I found was iteration15 of  
pomodo, which like the Agile examples, has no fixture data to actually  
do a test with. Has anyone actually tested this part of the Agile code  
in their app???

I should clarify that in the code above, test\_new also fails (in test  
only, app is fine), but if I add the (duplicate) line  
@borrower = Borrower.new(params[:borrower])  
to test\_new, the test doesn’t fail and the app still works fine.

I’m sure I’m missing something here, and I know I sure do miss my  
traditional debugging tools!

Shauna
