Behat test where exception is expected

Re: Behat test where exception is expected

by Andrew Lyons -
Number of replies: 0
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