Behat test where exception is expected

Behat test where exception is expected

by Tomasz Muras -
Number of replies: 4
Picture of Core developers Picture of Plugin developers Picture of Plugins guardians Picture of Translators

Hi,

Any idea how to write a test for the case where an exception is expected?

For example is stats are disabled, then going to a page: https://sandbox.moodledemo.net/report/courseoverview/index.php as non-admin will give an error:

Statistics are not enabled.

which is a Moodle Exception (printed with print_error()).

The above is the correct behavior, so I'd like my test to pass.

Average of ratings: -
In reply to Tomasz Muras

Re: Behat test where exception is expected

by Michael Hughes -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
You should be able to do an 'And I should see "Statistics are not enabled."' statement to check you got the exception
In reply to Tomasz Muras

Re: Behat test where exception is expected

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I believe it is not possible to test exceptions like this.
In reply to Tim Hunt

Re: Behat test where exception is expected

by Tomasz Muras -
Picture of Core developers Picture of Plugin developers Picture of Plugins guardians Picture of Translators
I think so as well Tim - I couldn't get it to work. Custom Moodle behat extension notices the exception in the output and fails the test.

It will not get to the next step, which could be "And I should see "Statistics are not enabled"...."
In reply to Tomasz Muras

Re: Behat test where exception is expected

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers



Now technically the answer is probably no but I think there be a way of doing this.


The reason I say it’s technically no is that the Moodle hq behat extension (found in the vendor directory) has a set of after steps which checks for any kind of exception.


In normal operation you cannot avoid this... however if you create a custom step which acts as a chained step where the API is called directly without making use of the self::execute() function and you have another step after that which navigates you to a different page, then I think that you can make it work.


Essentially I think my ou can write your custom step like:


public function i_should_see_exception($message) {

    $this->i_wait_for_page_to_load();

    $this->i_should_see($message);

    self::execute(‘behat_general::back’);

}


Now normally the i should see step is called using self::execute(). Behind the scenes that calls the wait for page to load part, then performs the operation, and finally check for exceptions. But if you call those things manually, as long as you love to a different page before the step finishes, it shouldn’t detect the exception.


Note: I am on mobile and have neither tested this nor can confirm the exact names of the functions. Also this is against our normal advice to use the execute function, but I believe it would work.


Andrew