Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails功能测试中的身份验证问题_Ruby On Rails_Ruby_Functional Testing - Fatal编程技术网

Ruby on rails rails功能测试中的身份验证问题

Ruby on rails rails功能测试中的身份验证问题,ruby-on-rails,ruby,functional-testing,Ruby On Rails,Ruby,Functional Testing,我正在测试此控制器: 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_u

我正在测试此控制器:

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
这个功能测试:

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
我会错过什么

编辑:这是“我的用户”表的结构(仅对图书示例使用纯文本密码)


伊甘,我99%确定问题在于密码不匹配。您是否使用加密/加密密码?如果是这种情况,请检查您的设备是否具有纯文本密码

我很有信心是这样的,因为你正在写一篇文章,得到一个200,而
create
中唯一返回200的路径是当用户/密码对验证失败时

如果您使用的是salt密码,您可能需要在您的设备中执行类似的操作

<% 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

一:
姓名:戴夫
哈希密码:
盐:
二:
姓名:MyString
哈希密码:MyString
盐:MyString

我正在浏览的这本书只有纯文本密码,例如,在我的sqlite文件中就是这样。让我试试你的解决方案,我会在没有工作后再联系你。我没有使用加密密码,因为这本书就是这样写的。有没有什么方法可以让我用纯文本密码来实现这一点?伊甘,你能把应用程序上传到什么地方吗?还有,你在用哪本书?正如您没有提到的标题:)只需通过Sitepoint创建Rails 2。有一些问题,因为这本书是在2008年写的,版本是Rails的2.0。对不起,我不能上传到任何地方。我已经开始工作了。我在test_helper.rb中丢失了一个fixture:all。我会接受这个答案,因为如果我的案例没有像在测试中漏掉一行那么愚蠢的话,它将是最合适的答案
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
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
)
<% 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