302 means redirect and you are redirecting?
I believe you can do
$response->assertRedirect($url);
https://laravel.com/docs/8.x/http-tests#assert-redirect
But remember to test if the action you expect is done
Summer Sale! All accounts are 50% off this week.
Hello,
I get an error on this part of the phpunit test:
AdsTest.php
public function test_input_slider_data()
{
$type = "slider";
$imagePath = UploadedFile::fake()->image('image.jpeg',1366,345)->size(100);
// Storage::fake('avatars');
$user = User::first();
$response = $this->actingAs($user)
->post('cpages/ads/'.$type,[
'ad_title'=>'slider 1',
'ad_caption'=>'slider 1',
'ad_url'=>'https://localhost/axe-backend-mg/public/home',// ok
'images'=>$imagePath
],[
'content-type'=>'multipart/form-data'
]);
$response->assertStatus(200);
}
AdsController.php
public function store(Request $request, $type)
{
if($type == 'slider')
{
$count = Advertisement::where('ad_type', 'slider')->count();
if($count == 1){
Session::flash('error', 'Tidak dapat menambah slider lebih dari 1');
return redirect()->back(); // this appears for response 302 response
}
}
elseif($type == 'featured_index')
{
$count = Advertisement::where('ad_type', 'featured_index')->count();
if($count == 3){
Session::flash('error', 'Tidak dapat menambah featured_index lebih dari 3');
return redirect()->back();
}
}
elseif($type == 'banner_ads')
{
$count = Advertisement::where('ad_type', 'banner_ads')->count();
if($count == 1){
Session::flash('error', 'Tidak dapat menambah banner_ads lebih dari 1');
return redirect()->back();
}
}
// $type = "slider";
if($type == "slider") {//fokus ke slider ini kan ya? iya
$this->validate($request, [
'ad_title' => 'required',
'ad_caption' => 'required',
'ad_url' => 'required',
'images' => 'required|dimensions:min_width=1360,min_height=345, max_width=1370,max_height=400'
]);
$dir_location = 'slider';
$dir = 'ads/slider/';
}elseif($type == "featured_index") {
$this->validate($request, [
'ad_title' => 'required',
'ad_caption' => 'required',
'ad_url' => 'required',
'images' => 'required|dimensions:min_width=400,min_height=225, max_width=415,max_height=230'
]);
$dir_location = 'featured_index';
$dir = 'ads/featured_index/';
}elseif($type== "banner_ads") {
$this->validate($request, [
'ad_title' => 'required',
'ad_caption' => 'required',
'ad_url' => 'required',
'images' => 'required|dimensions:min_width=1360,min_height=420, max_width=1370,max_height=425'
]);
$dir_location = 'banner_ads';
$dir = 'ads/banner_ads/';
}
$ads = new Advertisement;
$ads->ad_type = $type;
$ads->ad_title = $request->ad_title;
$ads->ad_caption = $request->ad_caption;
$ads->ad_url = $request->ad_url;
// Mengelola nama
$extension = $request->file('images')->extension();
$name_wo_extension = pathinfo($request->file('images')->getClientOriginalName(), PATHINFO_FILENAME);
// memberi dash kalo ada spasi namanya
$slug = Str::slug($name_wo_extension, '-');
$name = $slug . '.' . $extension;
// check the existance of old file
$count = count(Advertisement::where('ad_img_url', $dir.$name)->get());
while($count != 0)
{
$slug = $slug . '-copy';
}
$ads->ad_img_url = $dir . $name;
Storage::putFileAs($dir_location, $request->file('images'), $name);
$ads->save();
Session::flash('flash', 'Ads type :'. $type . ' created');
return redirect()->back();
}
ref: https://laracasts.com/discuss/channels/testing/expected-status-code-200-but-received-302
Any clue why the error appears?
Please or to participate in this conversation.