~~~~~~~~~~~~~~~ 修改/更新 ~~~~~~~~~~~~~~~
好的,我通过在更改块中的每个user.password_digest附加“.to_s”来修复未通过的测试。我认为这是有效的,因为在user.password_digest有点像指针之前,调用“to_s”会强制测试比较字符串值。如果有人能解释为什么测试比较指针而不是值,请赐教。
添加.to_s调用后(在我希望用户属性发生变化的地方添加),失败#3仍在发生(实际上是新的失败,但我确定它是由导致失败的同一问题引起的# 3)。为什么测试“使用正确的密码更正用户应该更改电子邮件”的任何想法都没有捕捉到在浏览器中正确发生的更改操作?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我正在尝试验证表单的行为以更新用户的个人资料(更改电子邮件和/或密码)。我的3个测试都失败了,我无法弄清楚原因。运行测试时,请查看控制台输出...
Failures:
1) User Pages Edit page correct user updates password with incorrect password should not change password
Failure/Error: expect { click_button "Save" }.not_to change{ user.password_digest }
result should not have changed, but did change from "$2a$10$Xrg1SHSyDmK5Of9HrjgYAuzkJzKk7HeaVjj4ZT9Ldz2R9BNji3D2S" to "$2a$10$Xrg1SHSyDmK5Of9HrjgYAuzkJzKk7HeaVjj4ZT9Ldz2R9BNji3D2S"
# ./spec/requests/user_pages_spec.rb:135:in `block (6 levels) in <top (required)>'
2) User Pages Edit page correct user updates password with confirmation mismatch should not change password
Failure/Error: expect { click_button "Save" }.not_to change{ user.password_digest }
result should not have changed, but did change from "$2a$10$bm2viuIuNCRIEEWe/qYVAOVl0r0EW7Y/1CqalKUwgR.ht/wVtQ8Zi" to "$2a$10$bm2viuIuNCRIEEWe/qYVAOVl0r0EW7Y/1CqalKUwgR.ht/wVtQ8Zi"
# ./spec/requests/user_pages_spec.rb:147:in `block (6 levels) in <top (required)>'
3) User Pages Edit page correct user updates email with correct password should change email
Failure/Error: expect { click_button "Save" }.to change{ user.email }.to(new_email)
result should have been changed to "new_email@example.com", but is now "person_7@example.com"
# ./spec/requests/user_pages_spec.rb:121:in `block (6 levels) in <top (required)>'
Finished in 2.99 seconds
8 examples, 3 failures
失败#1&amp; 2应该通过,对吗?字符串前后匹配,预计它们不会改变!可能会发生什么?
此外,我无法弄清楚为什么失败#3正在发生。
代码在浏览器中按预期工作。它可能与AJAX有关吗?表单正在点击的更新操作不会重定向到任何内容 - 它只是使用flash通知再次呈现页面。我是一个Rails新手,所以我可能会遗漏一些明显的东西。
表格:
<%= form_tag user_path(params[:id]), method: :put do %>
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] || @user.email %>
<%= label_tag :new_password %>
<%= password_field_tag :new_password %>
<%= label_tag :new_password_confirmation, "Confirm new password" %>
<%= password_field_tag :new_password_confirmation %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Save", class: "btn btn-primary pull-down" %>
<% end %>
测试规范(为清晰起见,删除了不相关的测试):
require 'spec_helper'
describe "User Pages" do
subject { page }
describe "Edit page" do
let(:user) { FactoryGirl.create(:user) }
describe "correct user" do
before do
log_in user # helper method that logs the user in, a la Michael Hartl tutorial
visit edit_user_path(user)
end
it { current_path.should == edit_user_path(user) }
it { should have_selector('title', text: 'Edit') }
it { should_not have_content('not authorized') }
describe "updates email" do
let(:new_email) { "new_email@example.com" }
before { fill_in 'Email', with: new_email }
describe "with incorrect password" do
it "should not change email" do
expect { click_button "Save" }.not_to change{ user.email }
end
end
describe "with correct password" do
before { fill_in 'Password', with: user.password }
it "should change email" do
expect { click_button "Save" }.to change{ user.email }.to(new_email)
end
end
end
describe "updates password" do
describe "with incorrect password" do
before do
fill_in 'New password', with: 'newpassword'
fill_in 'Confirm new password', with: 'newpassword'
end
it "should not change password" do
expect { click_button "Save" }.not_to change{ user.password_digest }
end
end
describe "with confirmation mismatch" do
before do
fill_in 'New password', with: 'newpassword'
fill_in 'Confirm new password', with: 'password'
fill_in 'Password', with: user.password
end
it "should not change password" do
expect { click_button "Save" }.not_to change{ user.password_digest }
end
end
describe "with correct password" do
before do
fill_in 'New password', with: 'newpassword'
fill_in 'Confirm new password', with: 'newpassword'
fill_in 'Password', with: user.password
end
it "should change password" do
expect { click_button "Save" }.to change{ user.password_digest }
end
end
end
end
end
end
相关宝石版本:Rails 3.2.9,Capybara 1.1.2,FactoryGirl 4.1.0