Having problem testing a form submission with attached file with Rspec and Capybara

66 Views Asked by At

Ruby in Rails.

I have a form where can upload an image in a file type input field along with other 3 text fields.

Everything works like a charm in production, development and so. BUT when I run a test that fills out those fields AND attach an image with attach_file and then hit the Submit button... the request never makes it to the controller method.

It goes to the root path '/' and shows nothing but a white and blank screen. And obviously the test fails.

If I attach nothing but the text fields then the request makes it to the controller.

Has anyone encountered this problem before?

Gemfile:

gem 'capybara', '~> 3.35', '>= 3.35.3'
gem 'cuprite', '~> 0.13'
gem 'factory_bot_rails', '~> 6.1'
gem 'rspec-rails', '~> 5.0.0'

spec.rb

require 'system_helper'

RSpec.describe 'Organization', type: :system do
  let(:user) { create :user }

  signed_in_as :user

  scenario 'User creates an organization with photo', js: true do
    visit new_organization_path

    fill_in 'organization[name]', with: 'Foo'
    fill_in 'organization[location]', with: 'Bar'
    fill_in 'organization[info]', with: 'Baz'

    # If this is commented the request goes successfully to the controller.
    page.attach_file(Rails.root.join('spec/fixtures/images/image.PNG')) do
      page.find('span', text: 'No File Selected').click
    end

    click_on 'Create Organization'

    # Should be redirected to this organization's view
    expect(page).to have_content 'Foo'
  end
end

Rails version: 6.1.4 Ruby: 3.1.3

0

There are 0 best solutions below