Use PHPUnit with interactive form

0

I have a form with Car Brands, Templates and Versions.

I'm using PHPUnit to test the form. It works this way:

User selects the tag, then the template select is loaded and finally the version select is loaded.

My application is made with Laravel 5.2 and I looked in the documentation how to create a test for forms and did so:

$this->visit('/compre-seu-carro')
     ->select('Chevrolet', 'marca_compra')
     ->select('Agile', 'modelo_compra')
     ->select('2010', 'ano_compra')
     ->select('Agile LT 1.4 8V (Flex) 2010', 'versao_compra')
     ->type('Dona Florinda', 'Nome')
     ->type('[email protected]', 'Email')
     ->type('999999', 'Telefone')
     ->select('melhor condicao a vista', 'troca')
     ->press('Avançar');

When I run PHPUnit it shows me this message:

PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

..E                                                                 3 / 3 (100%)

Time: 565 ms, Memory: 18.00MB

There was 1 error:

1) WebsiteTest::testNewBuyRegistration
InvalidArgumentException: Input "modelo_compra" cannot take "Agile" as a value (possible values: ).

/var/www/html/mapadocarro/vendor/symfony/dom-crawler/Field/ChoiceFormField.php:140
/var/www/html/mapadocarro/vendor/symfony/dom-crawler/FormFieldRegistry.php:122
/var/www/html/mapadocarro/vendor/symfony/dom-crawler/Form.php:77
/var/www/html/mapadocarro/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:576
/var/www/html/mapadocarro/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:556
/var/www/html/mapadocarro/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:543
/var/www/html/mapadocarro/tests/WebsiteTest.php:37

ERRORS!
Tests: 3, Assertions: 9, Errors: 1.

I think he is not loading the model and version selects. How could I fix this error to test form submission?

Form Ajax:

$('.select-brand').change(function() {
    var brand = $('.select-brand');
    if (brand.val() != '') {
        var model = $('.select-model');
        var idBrand = $('.select-brand').val();
        brand.attr('disabled', 'true');
        model.attr('disabled', 'true');
        model.html('<option value=""> Carregando modelos... </option>');
        $.get(url + '/get-models/' + idBrand, function (models) {
            model.empty();
            model.html('<option disabled selected value=""></option>');
            $.each(models, function (key,value){         
                if(!value){
                    model.html('<option value="">Nenhum modelo encontrado!</option>');
                }else{
                    model.append('<option value="'+ value.name +'">'+ value.name +'</option>');
                }

            });
            brand.removeAttr('disabled');
            model.removeAttr('disabled');
        });
    }else{
        var model = $('.select-model');
        model.empty();
        model.attr('disabled', 'true');
        model.html('<option disabled selected value=""></option>');

    }

});
    
asked by anonymous 13.01.2017 / 15:18

1 answer

0

To solve this problem you must change the way you are performing the test.

Using the Laravel Test API, you can not test features that are implemented in JS (this will be possible in version 5.4 through Dusk).

So, to test sending data from a form to the controller, I recommend that you use the post method of testing API:

$this->visit('url/da/sua/pagina');

$this->post('url/que/vai/receber/dados', [
    'inputA' => 'valorA',
    'inputB' => 'valorB',
])->followRedirects();

$this->see('Ver se mostrou mensagem de sucesso');

The post method will make a POST request to any url that you pass to it as the first argument. The second argument is the data that will go with the request, which in this case would be the form data. The followRedirects method is not required, but if you expect to be redirected after POST, you need to call this method.

    
23.01.2017 / 00:34