0

I'm having a problem with my Selenium test, it looks like the test data is not getting added to the right environment or Selenium is trying to read from the wrong one.

The below test passes without :js => true but with it it fails

it "allows the user to create a new test", :js => true do
    FactoryGirl.create(:user, email:'test@test.com', password:'passw0rd')

    p 'Here'
    p User.all

    visit '/' # Login view

    fill_in "email", with: 'test@test.com'
    fill_in "password", with: "passw0rd"
    click_button 'Login'

    expect(current_path).to eq '/home'
end

The test fails because the user isn't getting logged in and hence not reaching '/home'. I added print outs to see if the user was actually getting added to the database and it is...

"Here"
#<ActiveRecord::Relation [#<User id: 1, email: "test@test.com", password: "$2a$10$JQBuNH95JWDjCTx0OH7JMuxcO.0XgAz6wtYc/2G8pps...", created_at: "2014-08-27 11:34:44", updated_at: "2014-08-27 11:34:44">]>

However when I added another print out to my login_controller

def create
    p 'Here 2'
    p User.all
    ... # User authenitcation
end

The console looks like

"Here"
#<ActiveRecord::Relation [#<User id: 1, email: "test@test.com", password: "$2a$10$JQBuNH95JWDjCTx0OH7JMuxcO.0XgAz6wtYc/2G8pps...", created_at: "2014-08-27 11:34:44", updated_at: "2014-08-27 11:34:44">]>
"Here 2"
#<ActiveRecord::Relation []>

So for some reason the User that is being added in the test isn't in the Database at the time the authentication is taking place, leading me to think Selenium is looking at a different environment i.e development when it should be looking at test?

If it helps here is some of my spec_helper file with the database_cleaner config

...

RSpec.configure do |config|

    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.strategy = :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

Any thoughts on how to fix this? I don't know whether Selenium is looking at the wrong environment or my database_cleaner config is wrong

SteWoo
  • 458
  • 1
  • 8
  • 21

1 Answers1

0

The p output within the create action isn't too surprising - I believe JS tests run in different threads (the browser and the server are not synched up), so the state of the world very well might be different when the p in the create action is executed. One thing you could do to confirm that there is a timing issue is to add a hard wait in the test (like a sleep(3)) right after the click_button step.

Does that make sense?

dleve123
  • 100
  • 7
  • It's definitely not a timing issue as I've change Capybara's default wait time and watched the test as it runs (a flash message appears if the login isn't successful). Should the Selenium test still not read from the same environment i.e. `test` when the `login_controller` executes the `create` action? If not how are you supposed to test a page with JS that relies on Records being in the database? – SteWoo Aug 27 '14 at 19:28
  • I assume you have tried running the test in isolation? You could try removing database cleaner from the mix all together and manually reset the test database after each spec run by running `bundle exec rake db:test:prepare`. – dleve123 Aug 28 '14 at 00:10
  • Yep I'm running it on it's own. I removed Database Cleaner config and changed between transactional and none transactional fixtures with no luck. I also tried lots of different config combinations for Database Cleaner and still no luck. The problem seems to be that the data is in the model when printed out in the test, but there is no data in the model when printed out in the controller. Any thoughts why? – SteWoo Aug 28 '14 at 09:15