Wednesday, June 19, 2013

DropThot, my next web app.

I'm attempting to build a web application (dropthot.herokuapp.com) similar to ruminations.com before it closed down. Basically it's going to be an app where people can post their random day-to-day thoughts, upvote thoughts, and follow other users. More info on the project's Github page: http://github.com/alexsung/dropthot

This time I'm actually doing test-driven development, with RSpec, and trying out the new Rails 4.0!

I've been using Michael Hartl's tutorial (ruby.railstutorial.org) as a reference, since my app's functionality is somewhat similar to his. However, I've ran into a few issues so far.

For example, testing in RSpec:

describe "when email is already taken" do
    before do
      user_same_email = @user.dup
      user_same_email.name = "different_name"
      user_same_email.save
    end

    it { should_not be_valid }
end

Running this spec caused a problem, because the 4th line saves user_same_email to the test database, and then fails more specs the next run because of the persisting duplicate email address in the database.

Running "rake test:prepare" seems to fix it temporarily, but running the specs more than once results in a failure. I didn't have this issue when I was using Rails 3.2 on Michael Hartl's tutorial, so I'm assuming it's because of Rails 4. It makes sense because my tests run much faster in Rails 4 than in 3.2, so I'm guessing Rails 4 doesn't automatically clear the test database each time you run your specs. (edit/correction at the bottom of post)

I enjoy the fast test times since I can't get Spork to work with Rails 4, so I'm okay with this as long as I find a simple solution.

describe "when email is already taken" do
    before do
      @user_same_email = @user.dup
      @user_same_email.name = "different_name"
      @user_same_email.save
    end

    it { should_not be_valid }
    after { @user_same_email.destroy } # Changed to instance variable due to variable scope
end

That works.

Edit: It appears that the version of RSpec I was using (2.13.0) was the problem. I have updated my RSpec to 2.14.0.rc1 and it now works fine; my tests run equally fast and the test database clears between each run as expected.

No comments:

Post a Comment