How to run Request in Laravel unit test

WBOY
Release: 2016-09-02 08:57:04
Original
1075 people have browsed it

<code>class TestRequest()
{
    public function rules(){
        return [
            'a'=>['required','min:3'],
        ];
    }

}

//controller
public functino store(TestRequest $request)
{

}

Test.php
public function testStore()
{
    $data = [];
    $response = $this->call('POST','uri',$data);
    dd($response->getContent())
}
</code>
Copy after login
Copy after login

Every time if verification fails, $response->getStatusCode() is always 302, which is normal.
But I want to know, for example, if $data['a'] = 'a', the number of digits is less than 3, and I want to get its prompt information, how should I get it?
I am new to testing, please help me, thank you!

Reply content:

<code>class TestRequest()
{
    public function rules(){
        return [
            'a'=>['required','min:3'],
        ];
    }

}

//controller
public functino store(TestRequest $request)
{

}

Test.php
public function testStore()
{
    $data = [];
    $response = $this->call('POST','uri',$data);
    dd($response->getContent())
}
</code>
Copy after login
Copy after login

Every time if verification fails, $response->getStatusCode() is always 302, which is normal.
But I want to know, for example, if $data['a'] = 'a', the number of digits is less than 3, and I want to get its prompt information, how should I get it?
I am new to testing, please help me, thank you!

mwjmt mg

This test is so weird..
If you really want to get the error message, you can use Session::get. Because after verification fails, the errors object will be flash stored in the session.

For example, in your case:

<code>$response = $this->call(...);

$this->assertEquals(302, $response->getStatusCode());

dd(session('errors')->getBag('default')->first('a'));</code>
Copy after login

getBag obtains a MessageBag object. You can use get to obtain all verification error messages, or use first to obtain the first error message. For API documentation, see: http://laravel-china.org/api/...

--- Update

Generally, a test like this should be divided into at least two parts:
First test the validation part, using Validator, for example

<code>$data = ['a' => 'b'];
$request = new MyRequest();
$rules = $request->rules();
$validator = Validator::make($attributes, $rules);
$fails = $validator->fails();
$this->assertEquals(false, $fails);</code>
Copy after login

Then write the test method to test the logic behind it.

Regarding testing request in Laravel, the framework has provided many methods for testing. You can take a look at IlluminateFoundationTestingConcernsMakesHttpRequests. This basically covers all tests on request and response. Use other methods to test other parts, such as testing user authentication, testing cookie sessions, testing databases, testing queues, etc.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template