Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 迈克尔·哈特尔';错误教程-停留在第9章-can';我们无法通过RSpec测试_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 迈克尔·哈特尔';错误教程-停留在第9章-can';我们无法通过RSpec测试

Ruby on rails 迈克尔·哈特尔';错误教程-停留在第9章-can';我们无法通过RSpec测试,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我是一个绝对的编程初学者(刚刚开始钻研RubyonRails),这是我关于StackOverflow的第一篇文章,但是我事先做了彻底的研究,没有一个类似问题的答案能为我提供问题的解决方案 也就是说,在Michael Hartl的“使用Rails学习Web开发”教程第9.2.1章之后启动我的测试套件时,我收到了两次失败: Failures: 1) Authentication authorization for non-signed-in users in the Users controlle

我是一个绝对的编程初学者(刚刚开始钻研RubyonRails),这是我关于StackOverflow的第一篇文章,但是我事先做了彻底的研究,没有一个类似问题的答案能为我提供问题的解决方案

也就是说,在Michael Hartl的“使用Rails学习Web开发”教程第9.2.1章之后启动我的测试套件时,我收到了两次失败:

Failures:

1) Authentication authorization for non-signed-in users in the Users controller submitting to the update action 
 Failure/Error: before { patch user_path(user) }
 ActionController::ParameterMissing:
   param not found: user
 # ./app/controllers/users_controller.rb:39:in `user_params'
 # ./app/controllers/users_controller.rb:28:in `update'
 # ./spec/requests/authentication_pages_spec.rb:63:in `block (6 levels) in <top (required)>'

2) Authentication authorization for non-signed-in users in the Users controller visiting the edit page 
 Failure/Error: it { should have_title('Sign in') }
   expected #has_title?("Sign in") to return true, got false
 # ./spec/requests/authentication_pages_spec.rb:59:in `block (6 levels) in <top (required)>'

Finished in 2.35 seconds
70 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:64 # Authentication authorization for non-signed-in users in the Users controller submitting to the update action 
rspec ./spec/requests/authentication_pages_spec.rb:59 # Authentication authorization for non-signed-in users in the Users controller visiting the edit page 
这是我的/app/controllers/users\u controller.rb:

  class UsersController < ApplicationController
  before_action :signed_in_user, only: [:edit, :update]
  before_action :correct_user,   only: [:edit, :update]

  ...

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  # Before filters

  def signed_in_user
    redirect_to signin_url, notice: "Please sign in." unless signed_in?
  end

  def correct_user
    @user = User.find(params[:id])
    redirect_to(root_url) unless current_user?(@user)
  end
end
class UsersController
…我想登录页面会呈现在这里:

/app/views/sessions/new.html.erb:

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
<div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>

登录
新用户

如果需要在这里添加任何其他文件,请询问,因为我不确定我是否发布了所有需要的内容。第一个失败似乎与用户控制器中的强参数有关,尽管我逐点检查了修改后的代码,没有发现任何差异。在第二次失败的情况下,我不知道会出现什么问题,因为当我在浏览器中访问应用程序时,它似乎生成了正确的标题,但测试失败

提前感谢您为帮助我所做的一切努力


Michal

您的第一个错误是
ActionController::ParameterMissing
。这是因为
user\u params
方法调用
params.require(:user)
。这意味着如果未定义params[:user],它将引发异常

调用
patch user\u path(user)
时,未传入用户参数,导致异常。但是,您永远不应该访问
user\u params
,因为
signed\u-in\u-user
应该在之前运行并将您重定向到另一个页面

这让我推测您的测试用户已经登录。您可能已经在
身份验证页面\u spec.rb的前面部分的
调用中加入了
签名。有你?如果是这样,在登录时将其移动到
上下文中“
块中,规范应通过


同样,如果您的测试用户实际登录,您将看不到登录链接——因此修复该链接也将修复您的第二个错误。

非常感谢您,尼克!我只需要被指向正确的方向。你的指导方针已经足够了。
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
<div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>