Forum: Ruby on Rails Problem testing portion of code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick S. (Guest)
on 2005-12-01 13:03
Hi,

I'm getting a bit frustated here. I'm trying to test the following code
in my User controller:

def validate
    if params[:user_id] and params[:key]
      @user = User.find(:first,
                      :conditions => ["id= ? AND security_token = ? AND
token_expiry >= now()", params[:user_id], params[:key]])

      if @user
        @user.verified = 1
        @user.update
        flash[:notice] = "Uw account is geverifieerd. Welkom op onze
site."
        redirect_to :action => 'login'
      else
        flash[:notice] = "Uw account kon niet geverifieerd worden."
      end
    else
      redirect_to :action => 'signup'
    end
  end

validates get called like:
http://localhost:3000/user/validate?user_id=15&key...

I'm trying to test it as follows:

   def test_validate
     get :validate, { :user_id => 1000005, :key => "abc" }
     assert_equal 1, User.count
     assert_equal 1, User.verified
     assert_equal 1, ActionMailer::Base.deliveries.size
     assert_redirected_to :action => "login"
   end

The first line get :validate ... works. But the second line returns 6 (I
have 6 users in my user fixture), but according to my validate method it
should only return 1 user. I have tried different kind of things, but
cann't get the test to work.

Hope somebody has some helpful comments. Code improvements are always
greatly appreciated!

Kind regards,

Nick
christopher.k.hall (Guest)
on 2005-12-01 14:42
(Received via mailing list)
calling User.count is going to count all users records in your db.  it's
equivalent to: "select COUNT(*) from users" so it makes sense that if
you
have 6 user records that 6 will be returned.

here is what i would do

def test_validate
  user = User.find(1000005)
  get :validate, { :user_id => user.id :key => user.key }
  user(reload)
  assert_equal 1, user.verified
  assert_equal 1, ActionMailer::Base.deliveries.size
  assert_redirected_to :action => "login"
end
christopher.k.hall (Guest)
on 2005-12-01 14:46
(Received via mailing list)
ack...typo

that should be

get :validate, { :user_id => user.id :key => user.security_token }

sorry
Nick S. (Guest)
on 2005-12-01 15:03
Thanks Christopher,

it worked marvelleously!! Had to change user(reload) to user.reload .
And I caught the typo (my skills are improving, slowly :) )

Thanks again!
christopher.k.hall (Guest)
on 2005-12-01 16:32
(Received via mailing list)
No problem.  Glad I could help.
This topic is locked and can not be replied to.