Forum: Ruby on Rails Problem testing portion of code

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

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
        flash[:notice] = "Uw account is geverifieerd. Welkom op onze
        redirect_to :action => 'login'
        flash[:notice] = "Uw account kon niet geverifieerd worden."
      redirect_to :action => 'signup'

validates get called like:

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"

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,

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
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 => :key => user.key }
  assert_equal 1, user.verified
  assert_equal 1, ActionMailer::Base.deliveries.size
  assert_redirected_to :action => "login"
christopher.k.hall (Guest)
on 2005-12-01 14:46
(Received via mailing list)

that should be

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

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.