1

I want to test the function to add an image to the application. How can I do this. attach_file method is not work. I don't know why. This is model:

class Card < ActiveRecord::Base
  belongs_to :user
  has_attached_file :picture, styles: { medium: "300x300>", thumb: "100x100>" }
  validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/
  validates_attachment_size :picture, :in => 0.megabytes..2.megabytes
  validates :original_text, :translated_text, :review_date, :user_id, :picture, presence:   true

this is rspec:

it "check add card" do
    FactoryGirl.create(:card, user_id: @user.id)
    visit root_path
    click_on 'Add card'
    attach_file('card[picture]', File.join(Rails.root, '/spddec/features/test.png' ))
    ...
    ...
    click_on 'Create Card'
    expect(page).to have_content('тест')
  end

this is view:

<%= simple_form_for([current_user, @card]) do |f| %>
  <%= f.input :picture, label: 'Include Picture' %>
  <%= f.input :original_text, label: 'Original Text'  %>
  <%= f.input :translated_text, label: 'Translated Text' %>
  <%= f.input :review_date, label: 'Дата просмотра' %>
  <%= f.button :submit %>
<% end %>

 (HTML)
<input class="file required" id="card_picture" name="card[picture]" type="file" /></div>

and this is error which I get:

Failure/Error: FactoryGirl.create(:card, user_id: @user.id)
 ActiveRecord::RecordInvalid:
   Validation failed: Picture can't be blank
Vladimir Fomin
  • 343
  • 4
  • 14

1 Answers1

0

Try this code as this link:

page.attach_file(picture, '/path/to/file.png')

or this code as this link:

it "can upload a image" do
  post :picture, avatar: fixture_file_upload('files/spongebob.png', 'image/png')
  response.should be_success
end

Read answers of this question, I think this more helpful for your question.

Community
  • 1
  • 1
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45