简体   繁体   中英

Rails 4 devise integration testing. Persistent user?

I'm using devise for authentication in a small web application, and I'm having a few problems writing some integration tests.

The tests are going to be simple, such as

  • login works with valid credentials

  • login rejected with invalid credentials

  • ...

using the techniques that were described in the rails tutorial, but rather than against a home grown authentication system, I'm attempting to retrofit it against devise.

I can use the sign_in function without any problems, and I'm doing that in one or two of my controller tests, eg

require 'test_helper'

class mySimpleControllerTest < ActionController::TestCase

  include Devise::TestHelpers

  def setup
    @user = User.create!(
      :firstname => "ANOther",
      :surname => 'Person',
      :username=> 'aperson',
      :email => 'aperson@example.com',
      :password => 'pass123',
      :password_confirmation => 'pass123'
    )
    sign_in @user
  end

  test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "TEST PAGE"
  end
end

that works wonderfully well. The problem I have is my integration tests for testing the login functionality. I don't want both setup and teardown functions in there, since some tests will have to check against logged out behaviour, some against logged in, and others against reset password etc.

the following test is always is responding with invalid username and password, even though the passwords are correct. Eventually, I want this test to pass when the username or password is incorrect, but right now it responds this way regardless of whether it is or it isn't.

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  test "should be redirected if root_path is called when logged out" do
    get root_path
    assert_response :redirect
  end

  test "login with invalid information" do
    get new_user_session_path
    assert_template 'devise/sessions/new'
    post user_session_path, 'user[email]' => 'aperson@example.com', 'user[password]' => 'pass123'

...

I'm assuming the reason for this is because my test database doesn't contain a real user, in fact, the users table is currently empty. Which makes sense, since no user, it should respond with invalid username or password. However, if that is the case, how can I guarantee that the test database is populated with this default user when calling rake test ?

After attempting several ways of getting this to work, I eventually moved all tests over to RSpec and Capybara, which will allow me to post data to forms.

I did test whether or not there was an issue with persistent users and this seemed to not be the case. The problem seemed to be that

post user_session_path, 'user[email]' => ...

didn't seem to actually post anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM