Artur Upton Renault
Artur Upton Renault

Reputation: 123

FactoryGirl sequences broke my rspec tests (I think)

So I'm making an web app here using Rails 4, and if it helps I'm on a Mac using Mountain Lion, developing using Aptana Studio 3.

I'm trying to make a page that will list and paginate my application's users. I'm using will_paginate and bootstrap-will_paginate to do that. It works perfectly when I run rails server using sample users on my database, but when I run my tests using RSpec and FactoryGirl all my tests for pages requiring user authorization aren't working. So that's why I think the problem is FactoryGirl.

Previously, my factories.rb looked like this, since it only needed to create one user:

FactoryGirl.define do
  factory :user do
    username "example_user"
    password "foobar"
    password_confirmation "foobar"
  end
end

Now I changed it to do this:

FactoryGirl.define do
  factory :user do
    sequence(:username)  { |n| "Person_#{n}" }
    password "foobar"
    password_confirmation "foobar"
  end
 end

And here are the changes to my user_pages_spec file. It's the only thing I have changed aside from the factories file before the tests stopped passing. This is the diff for it:

   describe "index" do
-    before do
-      sign_in FactoryGirl.create(:user)
-      FactoryGirl.create(:user, username: "bananarama")
-      FactoryGirl.create(:user, username: "appleface")
+    let(:user) { FactoryGirl.create(:user) }
+    before(:each) do
+      sign_in user
       visit users_path
     end

     it { should have_title('All users') }
     it { should have_content('All users') }

-    it "should list each user" do
-      User.all.each do |user|
-        expect(page).to have_selector('li', text: user.username)
+    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|
+          expect(page).to have_selector('li', text: user.username)
+        end
       end
     end
   end

Is something missing? Anything else I should be looking at? Again, the tests that are failing are the ones on pages about authorization. These include some in my authorization_pages_spec.rb

Thanks a lot!

Upvotes: 3

Views: 4151

Answers (2)

Matt Schwartz
Matt Schwartz

Reputation: 3394

It might be easier to do

FactoryGirl.define do
  sequence(:username) { |n| "Person#{n}" }

  factory :user do
    username
    password "foobar"
    password_confirmation "foobar"
  end
end

and then

FactoryGirl.create(:user)

to get a sequenced username and

FactoryGirl.create(:user, username: "some_name")

when you want to customize it.

Upvotes: 3

Artur Upton Renault
Artur Upton Renault

Reputation: 123

Fixed it on my own!

I think factory-girl must have been getting confused when asked to use the same FactoryGirl sequence in different tests.

I just used my old factory, but added a separate sequence called username. I used that sequence to override the default username every time I require multiple users. Basically:

FactoryGirl.define do
  sequence(:username) { |n| "Person#{n}" }

  factory :user do
    username "example_user"
    password "foobar"
    password_confirmation "foobar"
   end
 end

I created single users as usual, and then for multiple users I did the following: 30.times { FactoryGirl.create(:user, username: FactoryGirl.generate(:username)) }

Hope this helps someone in the future (:

Upvotes: 5

Related Questions