How to test form request rules in Laravel 5?

2020-02-28 04:53发布

I created a form request class and defined a bunch of rules. Now I would like to test these rules to see if the behaviour meets our expectations.

How could I write a test to accomplish that?

Many thanks in advance for your answers!

Update: more precisely, I would like to write a unit test that would check e.g. if a badly formatted email passes validation or not. The problem is that I don't know how to create a new instance of the Request with fake input in it.

2条回答
疯言疯语
2楼-- · 2020-02-28 05:44

The accepted answer tests both authorization and validation simultaneously. If you want to test these function separately then you can do this:

test rules():

$attributes = ['aa' => 'asd'];
$request = new MyRequest();
$rules = $request->rules();
$validator = Validator::make($attributes, $rules);
$fails = $validator->fails();
$this->assertEquals(false, $fails);

test authorize():

$user = factory(User::class)->create();
$this->actingAs($user);
$request = new MyRequest();
$request->setContainer($this->app);
$attributes = ['aa' => 'asd'];
$request->initialize([], $attributes);
$this->app->instance('request', $request);
$authorized = $request->authorize();
$this->assertEquals(true, $authorized);

You should create some helper methods in base class to keep the tests DRY.

查看更多
smile是对你的礼貌
3楼-- · 2020-02-28 05:45

You need to have your form request class in the controller function, for example

public function store(MyRequest $request)

Now create HTML form and try to fill it with different values. If validation fails then you will get messages in session, if it succeeds then you get into the controller function.

When Unit testing then call the url and add the values for testing as array. Laravel doc says it can be done as

$response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
查看更多
登录 后发表回答