Rails 'post' command in rspec controllers: files fail. (Is there a multi-line setup?)

I am trying to run the following specification

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 

A validation error is generated for validates_presence_of: bannerimage - I narrowed it down as follows:

  • If I take validates_presence_of checks from bannerimage, it works, but the banner is reported as "missing.png"
  • Banner.create! (valid_attributes) works
  • I showed only one spec above, but the problem arises in any specification that includes this entry: create ,: banner => valid_attributes line
  • I selected all the links to attr_accessible ... no difference
  • I tried switching validate_attributes to Factory.attributes_for (: banner), with the same file information in: bannerimage
  • The form works fine through the browser, including uploading / processing images
  • File.Exists? confirms that the link file is indeed there.

If anyone has any ideas on why the message fails, I would really appreciate it. I guess (and I apologize - I did not look into the internal work of the "post" command and maybe here) that it skips some kind of "multipart" parameter in this call to receive files (?) ... I could not find anything through Google

Any ideas appreciated - I'm completely at a standstill.

The controller is a fully unmodified Rails 3.1 forest resource. The model is 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 
+6
source share
1 answer

Depending on your specific test setup, some combination of the following may occur instead of sending the File.open file

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

This function is defined by rails, and I believe that it is available with rspec-rails, although rails suggest using TestUnit

I used this in Cucumber definitions, it can work in rspec examples.

 Rack::Test::UploadedFile.new('spec/samples/laptop1.jpg', 'image/jpg') 
+18
source

Source: https://habr.com/ru/post/896463/


All Articles