0

I want to to testieng POST request. My controller's method look like this:

  public function store(Request $request)
  {
      $this->Validate($request, [
        'name' => 'required|string|min:5'
      ]);
      $product = Product::create([
        'name' => $request->name
      ]);
      return redirect()->back();
  }

So, I wrote this simple test but I have an error becouse it received 302 code:

$response = $this->post('/product/store', [
          'name' => 'Hello'
        ])
        ->assertStatus(201);

I think that problem is becouse I redirect page after store data. How I can testing this POST request?

2

1 Answer 1

1

For redirects you can assert a status of 302 (https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection)

$response = $this->post('/product/store', [
      'name' => 'Hello'
    ])
    ->assertStatus(302);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.