{"action":"created","issue":{"url":"https://api.github.com/repos/jnicklas/capybara/issues/1089","labels_url":"https://api.github.com/repos/jnicklas/capybara/issues/1089/labels{/name}","comments_url":"https://api.github.com/repos/jnicklas/capybara/issues/1089/comments","events_url":"https://api.github.com/repos/jnicklas/capybara/issues/1089/events","html_url":"https://github.com/jnicklas/capybara/issues/1089","id":14643978,"number":1089,"title":"Rspec after hook being called before spec finishs","user":{"login":"rafaels","id":77139,"avatar_url":"https://avatars.githubusercontent.com/u/77139?v=3","gravatar_id":"","url":"https://api.github.com/users/rafaels","html_url":"https://github.com/rafaels","followers_url":"https://api.github.com/users/rafaels/followers","following_url":"https://api.github.com/users/rafaels/following{/other_user}","gists_url":"https://api.github.com/users/rafaels/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaels/subscriptions","organizations_url":"https://api.github.com/users/rafaels/orgs","repos_url":"https://api.github.com/users/rafaels/repos","events_url":"https://api.github.com/users/rafaels/events{/privacy}","received_events_url":"https://api.github.com/users/rafaels/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":32,"created_at":"2013-05-22T20:34:57Z","updated_at":"2015-01-28T01:59:09Z","closed_at":"2013-10-20T18:12:43Z","body":"I am having trouble with a scenario where I only do:\r\n\r\n* click_link, which will fire an ajax request\r\n* find something\r\n* on a after(:each), I run DatabaseCleaner.clean\r\n\r\nThe problem is, the ajax request is running at the same moment of the after(:each) block, which causes me to have a nil association in the model code. \r\n\r\nIt is the real issue. I think that if I have a spec, it's after hooks should only run after all requests from this test are terminated. In my case I was getting controller and model code running at the same time as my DatabaseCleaner.clean, driving me to random errors when associations were not at the database anymore.\r\n\r\nI think it is a way important issue, cause I heard from people problems that fits it.\r\n\r\nFor example, if I have an js: true spec and inside this spec I interact with the page and some ajax requests are fired, but my spec isn't testing this specific ajax requests, this problem can easily arrive. Say this ajax request is slow and the real spec finished without needing the response from this ajax. The example will pass at the beginning but the ajax will fail the example later because it needs somethings at the database.\r\n\r\nPlease, if I can help more, get in touch.\r\n\r\nI am using capybara + rspec + poltergeist.\r\n\r\nPS. I resolved my problem with a better find, which will only be true when the ajax request returns and js renders the stuff. But this 'issue' showed me the above issue."},"comment":{"url":"https://api.github.com/repos/jnicklas/capybara/issues/comments/71767918","html_url":"https://github.com/jnicklas/capybara/issues/1089#issuecomment-71767918","issue_url":"https://api.github.com/repos/jnicklas/capybara/issues/1089","id":71767918,"user":{"login":"ka8725","id":243846,"avatar_url":"https://avatars.githubusercontent.com/u/243846?v=3","gravatar_id":"","url":"https://api.github.com/users/ka8725","html_url":"https://github.com/ka8725","followers_url":"https://api.github.com/users/ka8725/followers","following_url":"https://api.github.com/users/ka8725/following{/other_user}","gists_url":"https://api.github.com/users/ka8725/gists{/gist_id}","starred_url":"https://api.github.com/users/ka8725/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ka8725/subscriptions","organizations_url":"https://api.github.com/users/ka8725/orgs","repos_url":"https://api.github.com/users/ka8725/repos","events_url":"https://api.github.com/users/ka8725/events{/privacy}","received_events_url":"https://api.github.com/users/ka8725/received_events","type":"User","site_admin":false},"created_at":"2015-01-28T01:59:09Z","updated_at":"2015-01-28T01:59:09Z","body":"The problem arises because Capybara's hook in Rspec is executed before others. In my case it was `DatabaseCleaner`'s after hook and it was truncating the database randomly. BTW, `around` hook not worked for me either. So my solution is very simple - just past capybara's initial stuff into bottom of `spec/spec_helper.rb` file:\r\n\r\n```ruby\r\nENV['RAILS_ENV'] ||= 'test'\r\nrequire File.expand_path('../../config/environment', __FILE__)\r\nrequire 'rspec/rails'\r\n# Require here support files, other gems and init some other stuff\r\nRSpec.configure do |config|\r\nend\r\n\r\nrequire 'capybara/rspec'\r\nrequire 'capybara/poltergeist'\r\nCapybara.javascript_driver = :poltergeist\r\n# Other configuration of Capybara\r\n```\r\nHope it helped you either."}}