0
我試圖測試我的控制器使用水豚。我嘗試了很多方法,但出於某種原因,我無法看到current_user會話。我知道登錄工作時,我運行我的規範。我也在我的控制器測試之前訪問登錄頁面。但是,當:logged_in函數被擊中時,它似乎總是將我重定向。水豚/ Rspec控制器測試與has_secure_password
所以不知道我錯過了什麼?
這裏是我..
session_controller
def create
user = User.find_by_username(params[:username])
if(user && user.authenticate(params[:password]))
user.update_attribute(:token, User.token_digest)
flash[:notice] = "Success"
session[:user_id] = user.id
session[:token] = user.token
redirect_to dashboard_index_path
else
flash[:notice] = "Failed"
flash.now.alert = "Invalid user name or password"
render "new"
end
end
application_controller
protect_from_forgery
before_filter :logged_in
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def logged_in
if !current_user
redirect_to root_url
return
end
if session[:token] != current_user.token
redirect_to root_url
end
end
products_controller_spec
it 'Should display new product form' do
user_login
visit new_product_path
response.body.should have_content("<h1>Create New Product</h1>")
end
spec_helper.rb
def user_login
visit root_path #this is new_session
fill_in "username", :with => "admin"
fill_in "password", :with => "111111"
click_button "Login"
end