0
votes

I'm writing test code for Rails application with React. I'm using RSpec, Capybara, Poltergeist(PhantomJS 2.0) and Devise for authentication.

Because this is a javascript test, I'm also using database_cleaner gem as suggested here. When test runs I can see user data goes into database(mysql) and Devise method user.valid_password?("password") returns true, however, test fails.

spec_helper.rb

RSpec.configure do |config|

  config.use_transactional_fixtures = false

  Capybara.register_driver :poltergeist do |app|
    options = {
      inspector: true,
      js_errors: false,
      #debug: true,
      phantomjs_options: %w[
        --web-security=no
        --ignore-ssl-errors=yes
        --ssl-protocol=any
      ]
    }
    Capybara::Poltergeist::Driver.new(app, options)
  end

  Capybara.javascript_driver = :poltergeist

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

end

The spec looks like this.

require 'spec_helper'

feature 'Registration', js: true do
  let!(:email) { "[email protected]" }

  scenario "user can sign up and log in" do
    visit 'sign_up'
    find('input').set(email)
    find('.submit').click
    expect(page).to have_content('welcome!')

    visit '/sign_out'
    visit '/sign_in'

    expect(page).to have_content('Login')
    find('input[name="email"]').set(email)
    find('input[name="password"]').set('password')
    find('.submit').click
    wait_for_ajax

    expect(page).to have_content('Test User')
  end

end

API looks like this.

post do
  email = params[:email]
  password = params[:password]

  if email.nil? or password.nil?
    return error!(['this is error'], 401)
  end

  user = User.where(email: email.downcase).first
  if user.nil?
    return error!(['this is error'], 401)
  end
  if !user.valid_password?(password)
    return error!(['this is error'], 401)
  else
    user.ensure_authentication_token
    user.save
    return { status: 'ok', results: { access_token: user.authentication_token } }
  end
end

I get log like below with PhantomJS 2.0.

API.ajax:POST /sessions
API.ajax:POST /sessions
API.ajax:POST /sessions :failed
httpStatus:401
error
[object Object]
API.ajax:POST /sessions :failed
httpStatus:401
error
[object Object]

So something went wrong in authentication process but it only happens when I run test. (I can sign up and log in with Chrome or Firefox.)

Appreciate any idea.

Thanks!

1

1 Answers

0
votes

It turned out some functionality refers HARD CODED port 3000 when I run test..

rspec - Running rails server changes test result? - Stack Overflow

I don't know why it's built like that but at least for now I can run my test by adding Capybara setting: Capybara.server_port = 3000

Thanks!