看板 Ruby 關於我們 聯絡資訊
(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
poga:fixture :users 09/12 21:22
godfat:建議用 users :bob 來取 fixture, 你看的教學有點老了... 09/12 21:54