[英]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: 每个用户使用完全相同的代码和工厂,因此:
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.