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 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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With