Test_create gets 200 instead of :success

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

Get freelance or telecom-job here!!
http://freelance.telecom.googlepages.com

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

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!

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs