我正在编写测试以确认用户帐户在输入正确的密码后被删除。
我正在测试的表格:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :delete }) do |f| %>
<%= f.label :password %>Enter your password to delete your account.<br />
<%= f.password_field :password_to_delete %><br />
<button type="submit", data-test="delete-user-account">Delete my account</button>
<% end %>
([I can confirm the action works,但测试不。)
这是我当前的测试:
RSpec.feature 'user deletes account' do
let(:user) { create :user }
# when user puts in correct password and successfully deletes
context 'when successful' do
scenario 'user inputs correct password' do
visit new_user_session_path
fill_in 'user_email', with: user.email
fill_in 'user_password', with: '123456'
find('[data-test="submit"]').click
visit edit_user_registration_path
fill_in 'user_password_to_delete', with: '123456'
find('[data-test="delete-user-account"]').click
expect(user).not_to be_present
end
end
end
返回的内容:
Failures:
1) user deletes account when successful user inputs correct password
Failure/Error: expect(user).not_to be_present
expected `#<User id: 1>.present?` to return false, got true
我不是这方面的专家,但可以尝试
user.reload
expect(user).not_to be_present
逻辑原因是用户仍然作为变量而不是数据库而持久。
或者您可以使用
user = User.find_by_id user
#this will try to find the user from database which is already deleted and thus returning nil
expect(user).not_to be_present
我知道了!
我确定要在删除部分之前和之后检查User.last
,这将返回nil。
我什至还检查过是否更改了密码(如果密码返回的是假肯定密码,但不是)。>