我正在使用带有webkit驱动程序的capybara来进行集成测试,我想测试一个用户可以停用他的帐户覆盖一个设备控制器,覆盖工作,因为我可以通过绑定pry看到它确实改变了用户。 / p>
这是用户模型上的相关代码
class User < ActiveRecord::Base
[...]
def deactivate!
self.deactivated = true
self.deactivated_at = Time.zone.now
self.save!
# binding.pry here shows that the user with id 1 changed
end
end
我的设计破坏方法的覆盖,这是一个只更改了一行的复制粘贴
class RegistrationsController < Devise::RegistrationsController
def destroy
resource.deactivate!
# binding.pry shows that the user was actually deactivated
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :destroyed if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
end
end
现在问题的关键,测试
feature 'registration' do
let(:user) { create(:user) }
scenario 'user can deactivate his account' do
# binding.pry show that the user was created with id 1
# And now i perform steps to login the user
visit root_url
click_link 'Sign in'
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Log in'
# binding.pry shows that the user last_login_at was actually correctly updated, so the login works
# We need rack tests driver because the webkit driver does not support custom HTTP methods
current_driver = Capybara.current_driver
Capybara.current_driver = :rack_test
# Actual submit
page.driver.submit :delete, user_registration_path, {} # this triggers the deactivate method
Capybara.current_driver = current_driver
# binding.pry shows that user was not touched at all!
expect(user).to be_deactivated # so this fails :(
end
end
也许我错过了关于机架测试驱动程序的一些内容,并且它回滚了更改,看着tail -f log / test.log我看不到回滚但是我确实看到了 RELEASE SAVEPOINT active_record_1 在停用方法之后,任何想法?
答案 0 :(得分:3)
问题是在更改基础数据之前加载了规范中加载的用户对象。您需要重新加载用户对象才能查看所做的更改:
expect(user.reload).to be_deactivated