我是编程中的绝对初学者(刚开始钻研Ruby on Rails),这是我在StackOverflow上的第一篇文章,但是我做了一个事先进行彻底的研究,没有类似问题的答案为我解决了我的问题。迈克尔哈特尔的RoR教程 - 停留在第9章 - 无法让RSpec测试通过
也就是说,在迈克尔·哈特尔的9.2.1章后推出我的测试套件“了解使用Rails Web开发”教程中,我收到2个故障:
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
这里是/规格/请求/ authentication_pages_spec。 RB:
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
end
end
end
这里是我的/app/controllers/users_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
...和登录页面将在这里呈现,我想:
/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>
如果任何其他文件需要在这里添加,请问,因为我不确定是否发布了所需的一切。第一次失败似乎与用户控制器中的强参数有关,尽管我一点一点地检查了修改后的代码,发现没有差异。在第二次失败的情况下,我不知道可能会出现什么问题,因为当我在浏览器中访问它时,该应用似乎会生成正确的标题,但测试失败。
非常感谢您为帮助我而付出的努力。
米哈尔
非常感谢你,尼克!我只需要指出正确的方向。你的指导方针绰绰有余。 – michalronin
非常欢迎:) –