Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 'post' command in rspec controllers: Files aren't passing through. (Is there a multipart setting?)

I'm trying to run the following spec

describe "POST create" do
  describe "with valid params" do
    it "redirects to the created banner" do
      post :create, :banner => valid_attributes
      response.should redirect_to(admin_banner_url(Banner.last))
    end
  end
end


def valid_attributes
  demo_image = File.open(File.join(Rails.root, "spec", "samples", "laptop1.jpg"))
  {
    :name => 'Test Spec Banner',
    :bannerimage => demo_image
  }
end

The create is failing validation on validates_presence_of :bannerimage - I've narrowed it down as follows:

  • If I take the validates_presence_of validation off of bannerimage, it works, but the banner is reported as 'missing.png'
  • Banner.create!(valid_attributes) works
  • I've shown only one spec above, but the problem occurs on any spec which involves that post :create, :banner => valid_attributes line
  • I've taken out every reference to attr_accessible...no difference
  • I've tried switching validate_attributes into Factory.attributes_for(:banner), with the same file info in :bannerimage
  • Form works just fine through the browser, including image upload/processing
  • File.exists? confirms the referenced file is indeed there.

If anyone has any ideas on why post is failing, I'd be very appreciative. I'm guessing (and pardon - I haven't looked into the inner workings of the 'post' command and may be off here) that it's missing some sort of 'multipart' parameter on that call to accept files(?) ...couldn't find anything through google.

Any ideas appreciated - I'm completely stumped.

The controller is a completely unmodified Rails 3.1 scaffold resource. Model below.

class Banner < ActiveRecord::Base
  # attr_accessible :name, :url, :bannerimage

  has_attached_file :bannerimage, :styles => { :full => "960x", :thumb => "100x" }

  validates_attachment_content_type :bannerimage, :content_type => [ 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'], :message => 'file must be a gif, jpeg or png image'

  validates_attachment_size :bannerimage, :less_than => 3.megabytes

  validates_presence_of :name
  validates_attachment_presence :bannerimage
  validates_uniqueness_of :name

  has_many :pages, :dependent => :nullify

  def to_s
    name
  end
end
like image 518
PlankTon Avatar asked Sep 02 '11 07:09

PlankTon


1 Answers

Depending on your specific test setup some combination of the following might work instead of sending a File.open

fixture_file_upload('spec/samples/laptop1.jpg', 'image/jpg')

This function is defined by rails, and I believe it is available with rspec-rails even though rails expects you to use TestUnit

I've used this in Cucumber step definitions, it might work in rspec examples.

Rack::Test::UploadedFile.new('spec/samples/laptop1.jpg', 'image/jpg')
like image 79
danivovich Avatar answered Oct 14 '22 05:10

danivovich