无法通过水豚测试Devise [英] Failing to test Devise with Capybara

查看:45
本文介绍了无法通过水豚测试Devise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Devise构建一个Rails 3应用程序,并通过Capybara进行UI测试。以下测试失败:

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 => 'test@example.com', 
                         :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 'test@example.com', 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 => 'test@example.com'
    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中创建用户的方法相同。

... 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我习惯了Rails 2,使用Selenium进行这种测试,所以我怀疑我在做些愚蠢的事情。有人可以向我指出正确的方向吗?

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?

推荐答案

我遇到了同样的问题,并发现了具有解决方案的线程

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

要使DatabaseCleaner起作用,您需要包括 database_cleaner gem。如果您以前从未使用过它,则可能需要 rake db:test:prepare 才能重新运行测试。我希望这也对您有用!

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!

这篇关于无法通过水豚测试Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆