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>');
}
});