Testing with Behat, I run into a moodle exception

Testing with Behat, I run into a moodle exception

by Mariana Tuma -
Number of replies: 3

I'm trying to test a custom block, relationships, and I want to ensure students cannot access it, even if they go to the direct URL. To do this, I implemented a behat step:

 /** 
     * Goes to specified URL
     * @Given /^I go to "([^"]*)"$/
     */
     public function iGoTo($url) {
         try{
             $session = $this->getSession();
             $session->visit($url);
         } catch(\Exception $e){
             echo "Exceção: " . $e; 
         }   
     }


This is the scenario so far:


Background:
  Given the following "users" exist:
    | username | firstname | lastname | email |
    | student1 | Student   |     1    | stundent1@example.com |
  And the following "courses" exist:
    | fullname | shortname | id |
    | Course1  | c1        | 1  |
  And the following "course enrolments" exist:
    | user     | course | role    |   
    | student1 | c1     | student |

@javascript @wip
Scenario: Students cannot access the relationship page
  When I log in as "student1"
  And I am on homepage
  And I go to "http://150.162.242.121/mariana/unasus-cp/local/relationship/index.php?contextid=1"
  Then I should see "Usar relacionamentos e ver membros"


When I run this scenario, I get a moodle exception which interrupts the test:


Moodle exception: Sorry, but you do not currently have permissions to do that (Usar relacionamentos e ver membros) More information about this error

  

      Debug info:
       
      Error code: nopermissions
      
      Stack trace:
       
      line 786 of /lib/accesslib.php: required_capability_exception thrown
      line 32 of /local/relationship/index.php: call to require_capability()


which is expected, but I don't want my test to stop, I want to verify that the message is being displayed on the browser. Is there a way for me to do this?


EDIT: When I made the step definition, I didn't implement the try/catch block, and the error already existed.

Average of ratings: -
In reply to Mariana Tuma

Re: Testing with Behat, I run into a moodle exception

by Rajesh Taneja -

Hello Mariana,

As behat is supposed to catch any exception/debugging message thrown on the page, it checks after each step. (behat_hooks::i_look_for_exception()), so there is no way you can bypass this.


As this is a local plugin, rather then throwing exception, in debug mode you can return text saying this page is not accessible by user.  

Average of ratings: Useful (1)
In reply to Rajesh Taneja

Re: Testing with Behat, I run into a moodle exception

by Mariana Tuma -

Is there a way for me to catch or incorporate this exception into a step?

In reply to Mariana Tuma

Re: Testing with Behat, I run into a moodle exception

by Rajesh Taneja -

With current implementation, code is executed from moodle-behat-extension after every step, to catch any debugging or exception on page. So it's beyond the scope of step to catch exception.

Please feel free to open an issue on tracker, so step can allow exceptions to be caught. But with current way, you need to either fix your code to not throw exception or check valid state only.


Average of ratings: Useful (2)