简体   繁体   English

如何使用FactoryGirl创建多个记录? 当前图像仅应用于第一条记录

[英]How to create multiple records with FactoryGirl? Currently images are applied only to first record

I am getting strange behavior from a spec/ factory. 我从规格/工厂得到奇怪的行为。 What am I doing wrong? 我究竟做错了什么?

# feature spec 

feature 'avatars' do
  let(:user1) { create :user_with_profile, role: 1 }
  let(:user2) { create :user_with_profile, role: 2 }
  let(:user3) { create :user_with_profile, role: 3 }
  let(:user4) { create :user_with_profile, role: 4 } 

  scenario 'displays user avatars' do 
    expect(find("ul.users")).to_not have_selector "img.role1[title='#{user1.name}']"
    expect(find("ul.users")).to_not have_selector "img.role2[title='#{user2.name}']"
    expect(find("ul.users")).to_not have_selector "img.role3[title='#{user3.name}']"
    expect(find("ul.users")).to_not have_selector "img.role4[title='#{user4.name}']"
  end
end

# factory

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    password Faker::Internet.password(10, 20)
    trait :name do
      name Faker::Name.name
    end
    # images are handled with Carrierwave
    trait :avatar do
      avatar { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'test_image.jpg')) }
    end
    factory :user_with_profile, traits: [:name, :avatar]
  end
end 

# view
<ul class='users'>
  <% Users.all.each do |user| %>
    <%= content_tag :li, class: "user_#{user.id}" do %>
      <%= link_to user_path(user), title: user.name do %>
        <% if user.avatar? %>
          <%= image_tag user.avatar_url(:large), title: user.name, class: "role#{user.role}" %>
        <% else %>
          <%= content_tag :div, class: "role#{user.role}" do %>
            <%= user.initials %>
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>
</ul>

The test is failing: 测试失败:

Failure/Error: expect(find("ul.users")).to have_selector "img.role2[title='#{user2.name}']"
expected to find css "img.role2[title='Jakob John']" but there were no matches

I thought it odd that user1 was passing whereas user 2 was failing, so I got rspec to save_and_open_page . 我认为user1通过而用户2失败,这很奇怪,所以我将rspec传递到save_and_open_page

<ul class="users">
  <li class="user_1">
    <a title="Jakob Johnson" href="/users/1">
      <img title="Jakob Johnson" class= 'role1' src="/avatars/users/1/large/0000000005_35594307d1.jpg">
    </a>
  </li>
  <li class="user_2">
    <a title="Jakob Johnson" href="/users/2">
      <div class='role2'>JJ</div>
    </a>
  </li>
  ## user3 and user4 also have divs not imgs
</ul>

Exactly the same code and factory is used for each user, so: 每个用户使用完全相同的代码和工厂,因此:

  1. Why has FactoryGirl created user1 with an avatar, and the other users without? 为什么FactoryGirl创建了具有化身的user1,而其他用户却没有化身?
  2. Why has FactoryGirl created all users with the same name? 为什么FactoryGirl用相同的名称创建所有用户?

Am I using FactoryGirl incorrectly? 我使用了不正确的FactoryGirl吗? I can't work out what I'm doing wrong, and any advice appreciated. 我无法弄清楚自己在做什么错,任何建议都值得赞赏。

The reason FactoryGirl is creating all users with the same name is because the name value is being set when your factory is defined. FactoryGirl使用相同名称创建所有用户的原因是因为在定义工厂时正在设置名称值。 To get around this you should use lazy attributes: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#lazy-attributes 为了解决这个问题,您应该使用惰性属性: https : //github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#lazy-attributes

to do this use parenthesis in your name trait as follows: 为此,请在您的姓名特征中使用括号,如下所示:

trait :name do
  name { Faker::Name.name }
end

this will mean that a new name is generated each time a new instance is created. 这意味着每次创建新实例时都会生成一个新名称。

Unfortunately I can't see exactly what the problem is with your avatar. 不幸的是,我看不到您的头像到底出了什么问题。 One thing you could try is fixture_file_upload and see if that helps. 您可以尝试的一件事是Fixture_file_upload ,看看是否有帮助。

To use this you need to add include ActionDispatch::TestProcess in your spec helper. 要使用此功能,您需要在规范助手中添加include ActionDispatch::TestProcess Then try changing your avatar trait to: 然后尝试将您的头像特征更改为:

trait :avatar do
  avatar { fixture_file_upload(Rails.root.join('spec', 'support', 'images', 'test_image.jpg'), 'image/jpg') }
end

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FactoryGirl - 如何创建记录层次结构? - FactoryGirl - how to create hierarchy of records? FactoryGirl创建多个记录 - FactoryGirl creating multiple records 创建一个使用当前种子记录的FactoryGirl定义 - Creating a FactoryGirl definition that uses a currently seeded record 使用FactoryGirl定义多个自定义记录 - Define multiple custom records using FactoryGirl 如何使用FactoryGirl创建夹具ActsAsTaggableOn? - How to create a fixture ActsAsTaggableOn with FactoryGirl? FactoryGirl是否在每次创建时都创建一条记录; 从不在rspec中的控制台工作 - does FactoryGirl create a record on every create; works from console not in rspec Rails创建单个记录VS创建多个记录PERFOMANCE差异 - Rails create single record VS create multiple records PERFOMANCE difference Rspec,FactoryGirl:FactoryGirl.create in before(:all)不是持久记录? - Rspec, FactoryGirl: FactoryGirl.create in before(:all) isn't persisting records? 如何创建多个新记录,检查现有重复项以及将新记录或现有记录添加到联接表中 - How to create multiple new records, checking for existing duplicates, and adding new or existing record to join table FactoryGirl 和 RSpec:为规范创建具有所需的 Nested_attributes 的记录 - FactoryGirl and RSpec: create a record with required nested_attributes for specs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM