无法使用水豚测试 Devise

发布于 2024-11-18 05:28:26 字数 1629 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-11-25 05:28:26

我遇到了同样的问题,并找到一个包含解决方案的帖子

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

要使 DatabaseCleaner 功能正常工作,您需要包含 database_cleaner gem。如果您以前没有使用过它,则可能需要在重新运行测试之前rake db:test:prepare。我希望这对你也有用!

I was having the same issue and found a thread with a solution:

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

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 to rake db:test:prepare before rerunning your tests. I hope this works for you, too!

ゞ花落谁相伴 2024-11-25 05:28:26

我以前遇到过类似的问题。直接设置密码会产生一些奇怪的效果,因为它应该被加密并用盐存储——有时它对我有用,有时则不然。我很难记住哪些具体案例有问题。我建议按照以下顺序(为了简单起见)

  • 验证密码字段是否正确填写并作为正确的参数传递(如果您使用 Devise 的自动生成视图并且没有接触过它,则没有必要)
    • 如果您的网站可以在开发模式下运行(即没有登录错误),则只需启动并手动登录即可
    • 如果没有,请将 debugger 插入到 sessions_controller 中作为第一行。然后检查 params 并确保密码正确且在 params[:user][:password] 中。

      如果您没有覆盖 Devise 的 sessions_controller,那么您可以使用 bundle show devise 找到您的 Devise 路径。然后在(devise path)/app/controllers/devise/sessions_controller.rb

    • 中查找create操作

  • 更改您的测试设置以通过 Web 界面创建用户,以确保密码设置正确,然后尝试再次运行测试

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)

  • Verify that the password field is getting filled in properly and passed as the right param (not necessary if you're using Devise's autogenerated view and haven't touched it)
    • if your site can run in development mode (i.e. no log in bugs), then just boot it up and log in manually
    • If not, insert debugger as the first line in your sessions_controller. Then check params and make sure the password is correct and in params[:user][:password].

      If you didn't override Devise's sessions_controller, then you can find your Devise path with bundle show devise. Then look for the create action within (devise path)/app/controllers/devise/sessions_controller.rb

  • Change your test setup to create a user through the web interface, to ensure the password gets set properly, then try running your test again
橘亓 2024-11-25 05:28:26

我也遇到了同样的问题,其设置与您的设置非常相似。就我而言,在初始化程序中切换到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文