作者mrvh (vincent)
看板Ruby
标题[问题] 想问ROR一个测的问题?
时间Fri Sep 12 20:42:16 2008
(ctrl + y 删去不必要的行列)
环境:
Ruby 1.8.6 Rails 2.1.0
--------------------------------------
http://www.aidanf.net/rails_user_authentication_tutorial
在这个网页上的教学..
我不懂在class UserTest类别中的类变数@bob里好像是一个新建的变数,
哪为什麽在测试的时候@bob这个变数已经存在着一个值
来跟User.authenticate("bob", "test")回传的值作比较是否相等呢?
可能我有点误会了程式的意思?
--------------------------------------
(User model)
class User < ActiveRecord::Base
...
def self.authenticate(login, pass)
u=find(:first, :conditions=>["login = ?", login])
return nil if u.nil?
return u if User.encrypt(pass, u.salt)==u.hashed_password
nil
end
...
end
------------------------------------------
(Unit Test)
class UserTest < Test::Unit::TestCase
self.use_instantiated_fixtures = true
fixtures :users
def test_auth
#check that we can login we a valid user
assert_equal @bob, User.authenticate("bob", "test")
#wrong username
assert_nil User.authenticate("nonbob", "test")
#wrong password
assert_nil User.authenticate("bob", "wrongpass")
#wrong login and pass
assert_nil User.authenticate("nonbob", "wrongpass")
end
...........
end
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.138.249
1F:→ poga:fixture :users 09/12 21:22
2F:推 godfat:建议用 users :bob 来取 fixture, 你看的教学有点老了... 09/12 21:54