Problem testing portion of code


#1

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=4f0f73620459a199c2816afb32fd4599

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


#2

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


#3

ack…typo

that should be

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

sorry


#4

Thanks Christopher,

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

Thanks again!


#5

No problem. Glad I could help.