【问题标题】:FactoryGirl sequences broke my rspec tests (I think)FactoryGirl 序列打破了我的 rspec 测试(我认为)
【发布时间】:2013-08-23 00:37:11
【问题描述】:

所以我在这里使用 Rails 4 制作一个网络应用程序,如果它有帮助的话,我会在 Mac 上使用 Mountain Lion,使用 Aptana Studio 3 进行开发。

我正在尝试创建一个页面来列出和分页我的应用程序的用户。我正在使用 will_paginate 和 bootstrap-will_paginate 来做到这一点。当我在我的数据库上使用示例用户运行 rails 服务器时,它运行良好,但是当我使用 RSpec 和 FactoryGirl 运行我的测试时,我对需要用户授权的页面的所有测试都不起作用。所以这就是我认为问题出在 FactoryGirl 的原因。

以前,我的 factory.rb 看起来像这样,因为它只需要创建一个用户:

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

现在我将其更改为:

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

这里是对我的 user_pages_spec 文件的更改。在测试停止通过之前,除了工厂文件之外,这是我唯一更改的内容。这是它的差异:

   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

有什么遗漏吗?还有什么我应该看的吗?同样,失败的测试是关于授权页面上的测试。这些包括我的 authorization_pages_spec.rb 中的一些

非常感谢!

【问题讨论】:

    标签: ruby-on-rails ruby rspec factory-bot


    【解决方案1】:

    我自己修好了!

    我认为当被要求在不同的测试中使用相同的 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)) }

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

    【讨论】:

      【解决方案2】:

      这样做可能更容易

      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")
      

      当你想要自定义它时。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 2013-11-18
        • 2020-12-04
        • 1970-01-01
        相关资源
        最近更新 更多