我是铁杆新手,测试对我来说仍然有点神秘。我没有成功地搜索测试表单的方法而没有添加宝石。这就是我想要做的事情。
我的表单包含old_password,new_password和confirm_password字段。
<%= form_for @user, :url => "change_password" do |f| %>
<%= f.label :Current_Password %>
<%= f.password_field :old_password, class: 'form-control' %>
<%= f.label :New_Password %>
<%= f.password_field :new_password, class: 'form-control' %>
<%= f.label :Confirm_Password %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Change Password", class: "btn btn-primary" %>
<% end %>
我的路线
# Users
resources "users"
patch 'users/:id/change_password' => 'users#change_password'
我的用户控制器操作
def change_password
# Change password.
@user = User.find(params[:id])
if @user and @user.authenticate(params[:user][:old_password])
if params[:user][:new_password] == params[:user][:password_confirmation]
@user.update_attribute(:password_digest,
User.digest(params[:user][:new_password]))
flash[:success] = "Password changed sucesfully."
redirect_to @user
else
flash.now[:danger] = "Password and confirmation do not match."
render 'edit'
end
else
flash.now[:danger] = "Incorrect password."
render 'edit'
end
end
我的测试。它与Rails教程中的测试非常相似。这是我不理解的部分。
test "password change wrong password" do
get edit_user_path(@user)
assert_template 'users/edit'
# Submit wrong password.
patch user_path(@user), user: { old_password: "invalid",
new_password: "foobar11",
password_confirmation: "foobar11" }
# Flash not empty.
assert_not flash.empty?
# Redirected to edit page.
assert_template 'users/edit'
# Password not changed in database.
end
在assert_template&#39; users / edit&#39;
上测试失败expecting <"users/edit"> but rendering with <[]>
答案 0 :(得分:0)
我得到了它的工作,并将分享我的解决方案,以帮助他人。另外,我不认为我做了所有正确的Rails方式。
我正在修补错误的路线。我不得不修补到用户路径(@user),而必须修补&#34;#{user_path(@user)} / change_password&#34;。那看起来很难看。还有更好的方法吗?
我的全部测试是
test "password change wrong password" do
# Submit wrong password.
patch "#{user_path(@user)}/change_password", :user => {
:old_password => 'invalid',
:new_password => 'foobar11',
:password_confirmation => 'foobar11' }
# Show to edit page.
assert_template 'users/edit'
# Flash not empty.
assert_not flash.empty?
# Password not changed in database.
@user.save
@user.reload.authenticate("password")
end