【发布时间】:2020-08-28 00:30:13
【问题描述】:
我创建了一个 maatwebsite/excel 导入例程,我想对其进行测试。
maatwebsite/excel 测试页面除了伪造它之外没有向我提供任何其他信息。 但我需要上传我真正的 excel 文件,因为我想验证 excel 文件中的数据是否被正确处理。
这是我的上传输入字段和对应的按钮以点击端点/import
<form action="/import" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control-file file-path" name="fileToUpload">
</div>
<button type="submit" class="btn btn-primary">Import File</button>
</form>
在视图的控制器端,上传的文件将被处理和导入。
...
public function store(Request $request) {
$request->validate([
'fileToUpload' => 'required|file|max:4096|mimes:xls,xlsx',
]);
// start the import
Excel::import(new SheetNavigator, request()->file('fileToUpload'));
...
需要导入的文件位于我的测试环境下:
/tests
/files
/myexcel.xlsx
public function test_user_can_import_file() {
Excel::fake();
$datafile = new UploadedFile(
base_path('tests/files/myfile.xlsx'),
'myfile.xlsx',
'xlsx',
13071,
true);
$res = $this->post('/import', [
'fileToUpload' => $datafile
]);
// asserting, that everything works..
}
我需要一个测试来验证上传是否成功以及是否触发了导入例程。 我尝试了一切,从伪造任何东西到使用存储。
感谢您的任何帮助,谢谢!
克里斯
【问题讨论】:
-
分享您实际尝试过的内容会很有用。
-
哦,那应该是我的问题,我会更新它。
标签: laravel testing phpunit maatwebsite-excel