nPn
nPn

Reputation: 16738

How do I debug what seems like timing related rspec test fails?

I am going through the rails tutorial and I am up to a point where I have quite a few tests (124).

I am starting to run into an issue in which sometimes a set of tests will randomly fail, but when I try to reproduce them using the same seed, they pass. The fails seem to be mainly related to a FactoryGirl sequence used to generate unique user emails. Most of the time the tests all pass, and if I run with out providing a seed these fails popup maybe 10% of the time.

Here is an example of what I am seeing:

enter image description here

Here is the rspec test code that seems to sometimes fail

describe "pagination" do

      before(:all) { 30.times { FactoryGirl.create(:user) } }
      after(:all)  { User.delete_all }

      it { should have_selector('div.pagination') }

      it "should list each user" do
        User.paginate(page: 1).each do |user|
          page.should have_selector('li', text: user.name)
        end
      end
    end

Here is the factory:

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
  factory :micropost do
    content "Lorem ipsum"
    user
  end
end

Update: The issue is related to the test database not being cleaned up after some tests. I could (and did) find the tests that were creating the user. I decided to use the database_cleaner gem and configure it as recommended here: configuring database_cleaner

Upvotes: 1

Views: 261

Answers (1)

lsaffie
lsaffie

Reputation: 1814

Perhaps previous tests are setting these users. Try putting the User.delete_all in the before filter before creating them.

Upvotes: 1

Related Questions