RSpec is a feature for streamlining testing. Set the value in advance and generate an instance according to it.
factories/post.rb
FactoryBot.define do
factory :post do
#{ }The value of is referenced and saved.
text {"Is the text"}
#after method. After building the Post instance, attach an image.
after(:build) do |post|
post.image.attach(io: File.open('spec/fixtures/test_image.png'), filename: 'test_image.png', content_type: 'image/png')
end
end
end
Describe to attach an image to the image column of the Post instance. If you don't attach the image via HTTP request
io: File.open ('Specify image file location'), filename:'File name', content_type:'File type (image or text) / extension
It is described as.
that's all!
Recommended Posts