1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/spec/dsl/attach_file_spec.rb
Thorbjørn Hermansen fe6f5a1bde Wrote failing specs which highlights missing content-type with rack test driver
Don't know if this could have been written any better, guess it can as
this is my first "deep" look into Capybara. The problem is found in the
Capybara::Driver::RackTest; it doesn't forward the content type of the
file being uplloaded to Rack::Test::UploadedFile.new. UploadFile's
initializer sets the content type to text/plain as a default.

This will make for instance Paperclip's attachment validation fail when
testing with Cucumber and Capybara even though we are uploading a type
which should pass.
2010-01-07 23:43:11 +01:00

66 lines
2.3 KiB
Ruby

module AttachFileSpec
shared_examples_for "attach_file" do
describe "#attach_file" do
before do
@session.visit('/form')
end
context "with normal form" do
it "should set a file path by id" do
@session.attach_file "form_image", __FILE__
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
end
it "should set a file path by label" do
@session.attach_file "Image", __FILE__
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
end
end
context "with multipart form" do
before do
@test_file_path = File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__))
@test_jpg_file_path = File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__))
end
it "should set a file path by id" do
@session.attach_file "form_document", @test_file_path
@session.click_button('Upload')
@session.body.should include(File.read(@test_file_path))
end
it "should set a file path by label" do
@session.attach_file "Document", @test_file_path
@session.click_button('Upload')
@session.body.should include(File.read(@test_file_path))
end
it "should not break if no file is submitted" do
@session.click_button('Upload')
@session.body.should include('No file uploaded')
end
it "should send content type text/plain when uploading a text file" do
@session.attach_file "Document", @test_file_path
@session.click_button 'Upload'
@session.body.should include('text/plain')
end
it "should send content type image/jpeg when uploading an image" do
@session.attach_file "Document", @test_jpg_file_path
@session.click_button 'Upload'
@session.body.should include('image/jpeg')
end
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running { @session.attach_file('does not exist', 'foo.txt') }.should raise_error(Capybara::ElementNotFound)
end
end
end
end
end