webrat/spec/private/rails/attaches_file_spec.rb

97 lines
3.0 KiB
Ruby
Raw Normal View History

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2008-10-25 16:42:38 +00:00
describe "attach_file" do
before do
Webrat.configuration.mode = :rails
@filename = __FILE__
2008-07-25 23:11:56 +00:00
@uploaded_file = mock("uploaded file")
2008-11-23 05:22:49 +00:00
ActionController::TestUploadedFile.stub!(:new => @uploaded_file)
end
it "should fail if no file field found" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/widgets">
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
lambda { attach_file("Doc", "/some/path") }.should raise_error(Webrat::NotFoundError)
end
it "should submit empty strings for blank file fields" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } })
2008-11-23 05:22:49 +00:00
click_button
end
it "should submit the attached file" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="widget_file">Document</label>
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
2008-11-23 05:22:49 +00:00
attach_file "Document", @filename
click_button
end
it "should support collections" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="widget_file1">Document</label>
<input type="file" id="widget_file1" name="widget[files][]" />
<label for="widget_file2">Spreadsheet</label>
<input type="file" id="widget_file2" name="widget[files][]" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
2008-11-23 05:22:49 +00:00
attach_file "Document", @filename
attach_file "Spreadsheet", @filename
click_button
end
it "should allow the content type to be specified" do
2008-11-23 05:22:49 +00:00
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="person_picture">Picture</label>
<input type="file" id="person_picture" name="person[picture]" />
<input type="submit" />
</form>
</html>
2008-11-23 05:22:49 +00:00
HTML
2008-07-25 23:11:56 +00:00
ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times
2008-11-23 05:22:49 +00:00
attach_file "Picture", @filename, "image/png"
click_button
end
it "should support nested attributes" do
with_html <<-HTML
<html>
<form method="post" action="/albums">
<label for="photo_file1">Photo</label>
<input type="file" id="photo_file1" name="album[photos_attributes][][image]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/albums", { "album" => { "photos_attributes" => [{"image" => @uploaded_file}] } })
attach_file "Photo", @filename
click_button
end
2009-04-08 00:30:12 +00:00
end