无法使用水豚测试 Devise
我正在使用 Devise 构建 Rails 3 应用程序,并使用 Capybara 进行 UI 测试。以下测试失败:
class AuthenticationTest < ActionController::IntegrationTest
def setup
@user = User.create!(:email => '[email protected]',
:password => 'testtest',
:password_confirmation => 'testtest')
@user.save!
Capybara.reset_sessions!
end
test "sign_in" do
# this proves the user exists in the database ...
assert_equal 1, User.count
assert_equal '[email protected]', User.first.email
# ... but we still can't log in ...
visit '/users/sign_in'
assert page.has_content?('Sign in')
fill_in :user_email, :with => '[email protected]'
fill_in :user_password, :with => 'testtest'
click_button('user_submit')
# ... because this test fails
assert page.has_content?('Signed in successfully.')
end
end
...但我不知道为什么。从代码中可以看到,正在数据库中创建用户;我使用与在 seeds.rb 中相同的方法来创建用户。
如果我通过调试器运行测试,我可以在数据库中看到用户并验证页面是否正在加载。但仍然认证失败;我可以验证这一点,因为如果我更改断言来测试失败情况,测试就会通过:
# verify that the authentication actually failed
assert page.has_content?('Invalid email or password.')
我习惯了 Rails 2, &使用 Selenium 进行此类测试,所以我怀疑我在做一些愚蠢的事情。有人可以在这里指出正确的方向吗?
I'm building a Rails 3 app using Devise, with Capybara for UI testing. The following test is failing:
class AuthenticationTest < ActionController::IntegrationTest
def setup
@user = User.create!(:email => '[email protected]',
:password => 'testtest',
:password_confirmation => 'testtest')
@user.save!
Capybara.reset_sessions!
end
test "sign_in" do
# this proves the user exists in the database ...
assert_equal 1, User.count
assert_equal '[email protected]', User.first.email
# ... but we still can't log in ...
visit '/users/sign_in'
assert page.has_content?('Sign in')
fill_in :user_email, :with => '[email protected]'
fill_in :user_password, :with => 'testtest'
click_button('user_submit')
# ... because this test fails
assert page.has_content?('Signed in successfully.')
end
end
... but I have no idea why. As you can see from the code, the user is being created in the database; I'm using the same approach to create the user as I did in seeds.rb.
If I run the test through the debugger, I can see the user in the database and verify that the page is loading. But still the authentication fails; I can verify this because if I change the assertion to test for the failure case, the test passes:
# verify that the authentication actually failed
assert page.has_content?('Invalid email or password.')
I'm used to Rails 2, & using Selenium for this sort of testing, so I suspect I'm doing something daft. Could someone please point me in the right direction here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,并找到一个包含解决方案的帖子:
要使 DatabaseCleaner 功能正常工作,您需要包含
database_cleaner
gem。如果您以前没有使用过它,则可能需要在重新运行测试之前rake db:test:prepare
。我希望这对你也有用!I was having the same issue and found a thread with a solution:
For the DatabaseCleaner stuff to work you'll need to include the
database_cleaner
gem. If you haven't used it before, you may need torake db:test:prepare
before rerunning your tests. I hope this works for you, too!我以前遇到过类似的问题。直接设置密码会产生一些奇怪的效果,因为它应该被加密并用盐存储——有时它对我有用,有时则不然。我很难记住哪些具体案例有问题。我建议按照以下顺序(为了简单起见)
debugger
插入到sessions_controller
中作为第一行。然后检查params
并确保密码正确且在params[:user][:password]
中。如果您没有覆盖 Devise 的
sessions_controller
,那么您可以使用bundle show devise
找到您的 Devise 路径。然后在(devise path)/app/controllers/devise/sessions_controller.rb
中查找
create
操作I've run into a similar problem before. Setting the password directly has some weird effects because it's supposed to be encrypted and stored with a salt--sometimes it works for me and other times it doesn't. I have a hard time remembering which specific cases were problematic. I'd recommend the following, in this order (for simplicity)
debugger
as the first line in yoursessions_controller
. Then checkparams
and make sure the password is correct and inparams[:user][:password]
.If you didn't override Devise's
sessions_controller
, then you can find your Devise path withbundle show devise
. Then look for thecreate
action within(devise path)/app/controllers/devise/sessions_controller.rb
我也遇到了同样的问题,其设置与您的设置非常相似。就我而言,在初始化程序中切换到 ActiveRecord 会话解决了问题。
此外,请确保您致电@user.skip_confirmation!如果您在设备中使用“可确认”模块。
I had the same issue with a setup fairly similar to yours. In my case, switching to ActiveRecord sessions in the initializer solved the problem.
Additionally, make sure you call @user.skip_confirmation! if you are using the "confirmable" module in devise.