Topic: Trouble with Integration Tests on RoR Tutorial by Michael Hartl

Hi there,

I'm currently working through Michael Hartl's Ruby on Rails Tutorial and have made it through Chapter 8. (Really liking the tutorial by the way!) Having finished chapter 8, I've run into a snag: the final integration test in users_spec.rb is succeeding on the FIRST run, but failing on the SECOND. (You can see the section of code I'm referring to here --> … ful_signup The error that shows up in the console is this:

1) Users signup success should make a new user
 Failure/Error: response.should have_selector('div.flash.success',...

However, when looking down through all the red HTML that gets spit out, I found this:

<div id="error_explanation">
       <h2>1 error 
           prevented this user from being saved.</h2>
       <p>There were problems with the following fields:</p>
   <li>Email has already been taken</li>

Thus it seems to me that the reason the test runs once, and THEN fails is that the user is still in the test DB on the second run and is therefore causing a validation failure due to duplicate e-mails. I can verify this by changing the e-mail in the test after each try. As long as I feed it a new email, the test won't fail.

In trying to troubleshoot further, I cloned the tutorial code from GitHub and setup the app. The tests run fine. I can run the whole suite, or just users_spec.rb as many times as I want - no problem. Actually, to be honest, (and I know this seems really odd) the code from GitHub did fail a few times with the same email duplication error, but then after I rest the test db it worked fine. Believe me, I have done this in my app as well, but I still have the same issue. And yes, I've copied and pasted in the test code directly from the book, and also from the working GitHub code in an effort to weed out typos. No deal.

It would appear that there's something outside of the users_spec.rb file itself which is causing the problem, but I can't seem to find it. I'd love to move forward with the tutorial, but for the moment I'm stuck. Any help would be appreciated!

Re: Trouble with Integration Tests on RoR Tutorial by Michael Hartl

Well, it turns out that the solution to the above is fairly simple. The


line in


was set to false. At Michael Hartl's suggestion, I changed this to true and everything works beautifully. As an alternative, a helpful Stackoverflow user also suggested a gem called database_cleaner. ( This worked also.