~~~~~~~~~~~~~~~ 编辑/更新 ~~~~~~~~~~~~~~~
好的,我通过将“.to_s”附加到更改块中的每个 user.password_digest 来修复未能通过的测试。我认为这是可行的,因为在 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和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
相关 gem 版本:Rails 3.2.9、Capybara 1.1.2、FactoryGirl 4.1.0