我正在测试这个控制器:
class SessionsController < ApplicationController
def new
end
def create
@current_user = User.find_by_login_and_password(
params[:login], params[:password]
)
if @current_user
session[:user_id] = @current_user.id
if session[:return_to]
redirect_to session[:return_to]
session[:return_to] = nil
else
redirect_to stories_path
end
else
render :action => 'new'
end
end
def destroy
session[:user_id] = @current_user = nil
end
end
使用此fixture(users.yml):
patrick:
login: patrick
password: sekrit
name: Patrick Lenz
email: patrick@limited-overload.de
john:
login: john
password: gh752px
name: John Doe
email: john@doe.com
和这个功能测试:
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
def test_should_show_login_form
get :new
assert_response :success
assert_template 'new'
assert_select 'form p', 4
end
def test_should_perform_user_login
post :create, :login =>'patrick', :password => 'sekrit'
assert_redirected_to stories_path
assert_equal users(:patrick).id, session[:user_id]
assert_equal users(:patrick), assigns(:current_user)
end
end
然而,我得到了这个失败:
test_should_perform_user_login(SessionsControllerTest) [/home/ygamretuta/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.9/lib/action_controller/test_case.rb:119]:
Expected response to be a <:redirect>, but was <200>.
Expected block to return true value
登录逻辑正在运行,但我的测试失败了,我的路线到stories_path:
story_votes GET /stories/:story_id/votes(.:format) {:controller=>"votes", :action=>"index"}
POST /stories/:story_id/votes(.:format) {:controller=>"votes", :action=>"create"}
new_story_vote GET /stories/:story_id/votes/new(.:format) {:controller=>"votes", :action=>"new"}
edit_story_vote GET /stories/:story_id/votes/:id/edit(.:format) {:controller=>"votes", :action=>"edit"}
story_vote GET /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"show"}
PUT /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"update"}
DELETE /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"destroy"}
stories GET /stories(.:format) {:controller=>"stories", :action=>"index"}
POST /stories(.:format) {:controller=>"stories", :action=>"create"}
new_story GET /stories/new(.:format) {:controller=>"stories", :action=>"new"}
edit_story GET /stories/:id/edit(.:format) {:controller=>"stories", :action=>"edit"}
story GET /stories/:id(.:format) {:controller=>"stories", :action=>"show"}
PUT /stories/:id(.:format) {:controller=>"stories", :action=>"update"}
我能错过什么?
编辑:这是我的用户表的结构(它仅使用纯文本密码作为图书示例)
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"login" varchar(255),
"password" varchar(255),
"name" varchar(255),
"email" varchar(255),
"created_at" datetime,
"updated_at" datetime
)
答案 0 :(得分:3)
Ygam,我99%确定问题是密码不匹配。您使用的是盐渍/加密密码吗?如果是这种情况,请检查您的灯具是否有纯文本密码。
我非常有信心是这种情况,因为你正在做一个帖子并获得200,并且create
中返回200的唯一路径是用户/密码对验证失败的时候
如果您使用的是盐渍密码,则可能需要在灯具中执行类似的操作
<% SALT = "NaCl" unless defined?(SALT) %>
one:
name: dave
hashed_password: <%= User.encrypt_password('secret', SALT) %>
salt: <%= SALT %>
two:
name: MyString
hashed_password: MyString
salt: MyString