关于stackoverflow的问题已经有了一些可能的解决方案,但是似乎没有一个问题可以解决我的问题,因此我想再问一个问题,希望它足够具体,不要重复。
我正在尝试测试使用devise的自定义登录表单。环境包括
以下规格由于401 Unauthorized
响应而失败,再次显示登录信息并显示错误消息,提示凭据错误。
require 'rails_helper' feature 'Logging in' do background do FactoryBot.create :user, email: '[email protected]', first_name: 'John', password: 'password', password_confirmation: 'password' end scenario 'with correct credentials' do visit new_user_session_path within('#user-sessions--new') do find('#user_email').fill_in with: '[email protected]' find('#user_password').fill_in with: 'password' end find('input[name="commit"]').click expect(page).to have_content 'John' end end
暂时禁用参数过滤器以进行日志记录后,[
test.log
的检查,我可以看到密码已正确提交,并且在尝试登录之前已经创建了用户,但认证失败。进入交互式调试会话并尝试在水豚的启动浏览器中手动登录,登录也会失败。通过控制台创建同一用户时,电子邮件/密码登录在开发模式下有效。目前,没有使用其他可能影响登录行为的设计功能(例如confirmable
)。
到目前为止,我发现的大多数问题都建议以下几点:
禁用交易固定装置:
我的rails_helper.rb
已包含此内容;在Failing to test Devise with Capybara中提出了建议,在我开始实施水豚规格之前也已经存在;RSpec.configure do |config| config.use_transactional_fixtures = false end
配置数据库清洁程序:
RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with( :truncation, except: %w[ar_internal_metadata schema_migrations] ) end config.before :each do DatabaseCleaner.start end config.after :each do DatabaseCleaner.clean end end
现在,我没有其他想法,为什么这种情况可能会失败,除了可能在具有适当的数据库连接的情况下进行热测试的环境之外。如果有人有帮助的想法,我很乐意听到。
关于stackoverflow已经有一些问题,有可能的解决方案,但是似乎没有一个问题可以解决我的问题,因此,我要再问一个问题,希望它足够具体,不要以...结尾]。 [
如果没有帮助,请在您的问题中添加test.log的相关部分
注意:您编写的测试比所需的要冗长。如果通过id将字段填充为fill_in,则只需将id传递给fill_in
,而不必为每个元素使用单独的find
调用,则当您尝试点击该元素时,还应该使用更具语义的click_button
符合按钮的资格]
within('#user-sessions--new') do
fill_in 'user_email', with: '[email protected]'
fill_in 'user_password', with: 'password'
end
click_button('commit')
expect(page).to have_content 'John'