3

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!

4

2 回答 2

5

我自己修好了!

我认为当被要求在不同的测试中使用相同的 FactoryGirl 序列时,工厂女孩一定会感到困惑。

我只是使用了我的旧工厂,但添加了一个名为 username 的单独序列。每次我需要多个用户时,我都会使用该序列覆盖默认用户名。基本上:

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

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

我像往常一样创建了单个用户,然后为多个用户执行了以下操作:30.times { FactoryGirl.create(:user, username: FactoryGirl.generate(:username)) }

希望这对将来的某人有所帮助(:

于 2013-08-23T01:36:31.437 回答
3

这样做可能更容易

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

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

接着

FactoryGirl.create(:user)

得到一个有序的用户名和

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

当您想要自定义它时。

于 2014-01-23T21:54:26.170 回答