Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

ruby on rails 3 - Failing to test Devise with Capybara

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...