Topic: functional testing

controller:
def list
if session['user']
    @contact_pages,@contacts = paginate :contacts,:conditions => ['introduced_by=?',@session['user'].id], :per_page => 10
   else
  puts "welcome"
  end
end


list.rhtml:

<th>FirstName</th>
    <th>IntroducedBy</th>
    <th>Organization</th>
    <th>Access</th>
   
  </tr>
       <%@contacts.each do |contact|%>
        <tr>
           
            <td><%=contact.first_name%></td>
            <td><%=contact.user.first_name %></td>
            <td><%=contact.organization.organization%></td>
             <td><%=contact.access.access%> </td>
               </tr>
      <%end%>

In functionaltest:
def test_list

    get :list ,:id=>2
    assert_response :success
    assert_template 'list'

    assert_not_nil assigns(:contacts)
  end

when i test this code i got error like




Exception: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
On line #42 of app/views/contacts/list.rhtml

       
    42:     <%@contacts.each do |contact|%>
    43:         <tr>
    44:             
    45:             <td><%=contact.first_name%></td>

    #{RAILS_ROOT}/app/views/contacts/list.rhtml:42:in `_run_rhtml_47app47views47contacts47list46rhtml'
    C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `send'
    C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `compile_and_render_template'
    C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:301:in `render_template'

plz any one give me reply  soon

Re: functional testing

session['user'] isn't set so that if conditions fails meaning it prints "welcome" instead. It still tries to render the template though. That's what raises the error because @contacts is not set.

Railscasts - Free Ruby on Rails Screencasts

Re: functional testing

ryanb wrote:

session['user'] isn't set so that if conditions fails meaning it prints "welcome" instead. It still tries to render the template though. That's what raises the error because @contacts is not set.

how can i solve this problem

Re: functional testing

It depends on what you are trying to do. The common thing to do is put a before_filter on the action which redirects the user to another page if he's not logged in. This way you don't have the "if" condition in the action.

Railscasts - Free Ruby on Rails Screencasts

Re: functional testing

swapna,

I'm still new to the RoR world and have been fighting writing tests for the last three weeks, but it seems to me that this line in your functional test is the issue:

get :list ,:id=>2

When sending a [bold]get[/bold] or [bold]post[/bold], the second argument essentially gets passed as part of the request.  In your case it'll be like typing this in the address bar in a web browser:

I think what you're trying to do is to set a session variable, which can be done with the third argument, like this:

get :list, {}, :id => 2

Let us know if that helps,


c.