1
我正試圖在Mojolicious中編寫測試以檢查圖像附件是否從窗體正確發送。 我有一個包含<input type="file" name="image_upload" accept="image/*">
的表單。在控制器中,我正在檢查$self->req->upload('image_upload')->headers->content_type =~ /image/
。另一方面,測試在發佈請求內發送圖像文件form => {image_upl => { content => 'abc', filename => 'x.png' }}
。運行該應用程序運行良好,但該檢查在測試代碼內部失敗。我不太瞭解如何設置這些標題,以便我可以在發佈請求中發送模擬圖像。在Mojolicious中編寫用於檢查文件附件類型的測試失敗
下面是代碼:
#Router.
$if_admin->post('/attachment/:page_id/add')->name('on_attachment_add')
->to('attachment#on_add');
# Client side (Mojolicious template).
<%= form_for 'on_attachment_add' => { page_id => $self->stash('id') }
=> (method => 'POST') => (enctype => "multipart/form-data")
=> begin %>
<input type="file" name="image_upload" accept="image/*">
% end
# Controller (on_add)
my $self = shift;
my $image_file = $self->req->upload('image_upload');
if (!$image_file || $image_file->slurp eq "" || !$image_file->headers ||
!$image_file->headers->content_type ||
!($image_file->headers->content_type =~ /image/)) {
$self->render(
template => 'validation/invalid_data',
status => 400
);
}
# test.
$t->post_ok('/attachment/test_page/add' => form => {
image_upload => { content => 'aaa', filename => 'x.png' },
image_name => 'new_image.jpg'
})->status_is(200);
這個巨大的條件很難閱讀。不是很好維護。 – simbabque
嗯simbabque你是對的,我試圖獲得MVP,所以我把所有的檢查都放在那裏。我正在改變它更清楚 – Bianca