1

我的应用程序使用 Carrierwave + Fog 在 Amazon S3 上存储图像。我对使用 Capybara 的attach_file助手添加文件的注册过程进行了 Rspec 功能测试。规范如下所示:

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

存根似乎工作正常,但由于控制器中的错误导致测试失败:

  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>'

这是控制器的外观:

  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

第 36 行if @business.save是调用的行。使用binding.pry,我注意到logo属性 on@businessnil在第 35 行实例化之后,我猜这与对 AWS 的存根请求没有返回任何 CarrierWave 需要分配属性。不过,我不确定如何解决这个问题以使测试通过。提前感谢您的帮助!

4

0 回答 0