Checking whether correct elements were added to a form for Unit Tests

Checking whether correct elements were added to a form for Unit Tests

από Ramindu Deshapriya -
Αριθμός απαντήσεων: 3

Hi,

I'm writing a Unit Test for a function that takes a form object and adds elements to it. To test it, I wrote my own class that extends moodleform within the test class itself, and added the same elements as the function is supposed to add, and asserted whether the two were equal. So for example, what I'm doing is similar to this: 

$expectedform = new my_form();
$expectedform->addElement('html', 'test data');
$actualform = new my_form();
some_function_that_changes_the_form($actualform);
$this->assertEquals($expectedform, $actualform);


Is there a better way to do this? 

Μέσος όρος βαθμολογίας: -
Σε απάντηση σε Ramindu Deshapriya

Re: Checking whether correct elements were added to a form for Unit Tests

από Davo Smith -
Φωτογραφία Core developers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Peer reviewers Φωτογραφία Plugin developers

This sounds like it might be easier to run as a Behat test, rather than a PHPUnit test. That way you could fire up the form and test that the expected elements were present, then submit the form, reopen it and check the expected values saved correctly.


Σε απάντηση σε Ramindu Deshapriya

Re: Checking whether correct elements were added to a form for Unit Tests

από Tim Hunt -
Φωτογραφία Core developers Φωτογραφία Documentation writers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Peer reviewers Φωτογραφία Plugin developers

I agree with Davo, this does not seem to be a very interesting thing to test. Is it the type of thing you are likely to get wrong in your code? If you are not likely to make mistakes, then no point testing.

Better to spend your time testing the validation method.

Σε απάντηση σε Tim Hunt

Re: Checking whether correct elements were added to a form for Unit Tests

από Ramindu Deshapriya -

Davo and Tim, I think you're right, will probably move these in to Behat tests - it doesn't make sense to unit test this functionality when I could very easily test form functionality using Behat. 

Thanks for the replies!