WebMock:未定义的方法`gsub!' for nil:使用`stub_request`

时间:2015-07-11 19:46:31

标签: ruby-on-rails rspec amazon-s3 carrierwave webmock

我的应用正在使用Carrierwave + Fog在Amazon S3上存储图像。我对注册过程进行了Rspec功能测试,该测试使用Capybara的attach_file助手来添加文件。规范看起来像这样:

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上的@business属性在第35行实例化后为nil而且我猜测这必须是处理对AWS的存根请求,而不返回CarrierWave分配属性所需的任何内容。我不知道如何解决这个问题,以便测试通过。在此先感谢您的帮助!

0 个答案:

没有答案