My app is using Carrierwave + Fog for storing images on Amazon S3. I have an Rspec feature test for the registration process that uses Capybara's attach_file
helper to add the file. The spec looks like this:
feature 'Registration' do
before do
stub_request(:any, /amazonaws/)
end
scenario 'New business signs up' do
visit new_user_registration_path
fill_in 'Name', with: 'my business'
attach_file 'Logo', with: "#{Rails.root}/tmp/image.png"
click_button 'Sign up'
expect(page).to have_text("Business successfully created.")
end
end
The stub seems to be working correctly but the test is failing because of an error in the controller:
1) Registration
Failure/Error: click_button 'Create Business'
NoMethodError:
undefined method `gsub!' for nil:NilClass
# ./app/controllers/businesses_controller.rb:36:in `create_with_user'
# ./spec/features/registration/signup_spec.rb:32:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
Here is what the controller looks like:
def create_with_user
@business = Business.new(business_params)
if @business.save
redirect_to business_url(@business), success: 'Business was successfully created.'
else
render :new_with_user
end
end
Line 36 is the line where if @business.save
is called. Using binding.pry
, I've noticed that the logo
attribute on @business
is nil
after it is instantiated in line 35 and I'm guessing this has to do with the stubbed request to AWS not returning whatever CarrierWave needs to assign the attribute. I'm not sure how to solve this so that the test passes, though. Thanks in advance for any help!