How to do the unit test in the class validator in a Laravel form?

0

A good day, gentlemen.

I am a student of programming, at that moment I am studying how to make unit tests using Laravel 5.3

In my tests I came across the following problem:

My little system only aims to register products in a database. After inserting the data into the forms send to my store function of my control where I make my validations:

public function store(Request $request)
{
    $this->validate($request, [
        'nome' => 'bail|required|unique:produtos|min:2',
        'valor' => 'bail|required|min:0.1|numeric|'
    ]);
    Produto::create($request->all());
    return redirect('produtos');
}

I would like to make tests that capture if these validates are actually working, but I am not able to do this.

Follow the code below:

class ProdutoTest extends TestCase
{

use DatabaseTransactions;
public function testExample()
{

    $this->visit('produtos/create')
         ->type("a", 'nome')
         ->type(15, 'valor')
         ->press('Cadastrar')
         ->seePageIs('produtos/create');



    Produto::create([
        'nome' => '45',
        'valor' => '77'
    ])  ;
}

}

By what I could understand in this test I am just testing my form, and creating data entered directly into the database.

How do I test my validates?

PS: "The names and organization of the test code are pretty clueless, since I have not set the haha ..."

    
asked by anonymous 30.07.2018 / 16:50

1 answer

0

Galera searching the internet I believe I have found a satisfactory solution

Follow the Code:

public function testNomeComUmCaracter()
{

    $this->visit('produtos/create')
         ->type('h', 'nome')
         ->type(15, 'valor')
         ->press('Cadastrar')
         ->notSeeInDatabase('produtos', ['nome' => 'h'])
         ->seePageIs('produtos/create');
}

In this test simulated one registration attempt putting a name with only one character, in our example "h", such insertion will meet with my validates:

public function store(Request $request)
{
$this->validate($request, [
    'nome' => 'bail|required|unique:produtos|min:2',
    'valor' => 'bail|required|min:0.1|numeric|'
]);
Produto::create($request->all());
return redirect('produtos');
}

I use the command: notSeeInDatabase With so I'm trying to visualize the data'm not contained in my BD out, what means that my validates killed tentantiva to insert

Finally I use the command: use DatabaseTransactions What did will cause my bank is not populated in fact, since the test firing at the database for the test, and then performs a rollback, leaving traces as little spending my IDs.

    
01.08.2018 / 12:58