Use Phpunit with interactive form

Asked

Viewed 228 times

0

I have a form with Brands, Models and Versions of Cars.

I’m using Phpunit to test the form. It works that way:

User selects the brand, then the select of templates is loaded and at the end the select of versions 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 go to excommunicate Phpunit he 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 don’t think it is loading the template and version selects. How could I resolve this error to test the form submission ?

Ajax of the form:

$('.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>');

    }

});
  • there is some ajax request?

  • The element <select name="modelo_compra"> or <input type="radio" name="modelo_compra"> is there? Post the HTML content.

  • @Rafaelacioly Yes !

  • @Guilhermenascimento It is a select that is empty until the choice of the brand and then populated with the models of the brand.

  • The test is happening before the data is placed in select.

  • https://laravel.com/docs/5.2/testing#testing-json-Apis

  • But you are using name or id html? Put the page HTML structure /compre-seu-carro. Detail, I’m pretty sure you don’t recognize Javascript by testing, so you’d have to use route testing or simulate the data population in select

Show 2 more comments

1 answer

0

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

Making use of the Laravel test API, it is not possible to test features that are implemented in JS (This will be possible in version 5.4 through Dusk).

So, to test the sending of data from a form to the controller, I recommend you use the method post of the test 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 method post will make a POST request for any url 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 method followRedirects is not required, but if you expect to be redirected after doing the POST, you need to call this method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.