webrat/spec/attaches_file_spec.rb
Kyle Hargraves ce4dc1cd47 Add attaches_file method
Note that file submissions in integration tests are only supported
on edge rails after revision 8978. Rick DeNatale has backported
support to 2.0.2; see his blog entry at:

http://talklikeaduck.denhaven2.com/articles/2008/04/18/rails-integration-test-file-upload-plugin
2008-04-28 05:00:08 -04:00

63 lines
2.2 KiB
Ruby

require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
describe "attaches_file" do
before do
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
@filename = __FILE__
@uploaded_file = mock
ActionController::TestUploadedFile.stubs(:new).returns(@uploaded_file)
end
it "should fail if no file field found" do
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/widgets">
</form>
EOS
lambda { @session.attaches_file("Doc", "/some/path") }.should raise_error
end
it "should submit empty strings for blank file fields" do
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/widgets">
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => "" } })
@session.clicks_button
end
it "should submit the attached file" do
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/widgets">
<label for="widget_file">Document</label>
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => @uploaded_file } })
@session.attaches_file "Document", @filename
@session.clicks_button
end
it "should support collections" do
@response.stubs(:body).returns(<<-EOS)
<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>
EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
@session.attaches_file "Document", @filename
@session.attaches_file "Spreadsheet", @filename
@session.clicks_button
end
end