How to implement the expected exceptions?

I am trying to use my first functions with Behat, and I ran into a problem, I do not know how to implement the expected exceptions.

I found the problem https://github.com/Behat/Behat/issues/140 , and robocoder talks about one possible way that Behat also uses. But it seems like they can't handle exceptions.

My goal is to force the handling of exceptions. I do not want any design to catch all exceptions and forget them.

One possible way:

When <player> transfers <transfer> from his account it should fail with <error>

Implementation

try {
    ...
} catch (\Exception $ex) {
    assertEquals($error, $ex->getMessage());
}

I do not like the description of the script. I want to use the then keyword , for example

When <player> transfers <transfer> from his account
Then it should fail with error <error>

This description has a drawback. I need two methods:

method1($arg1, $arg2) {
    // Test the transfer
}

method2($arg1, $arg2) {
    // Check if the exception is the right one
}

2, .
, , - try/catch .
- . .

?
- ?

.

:

playerTransfer($player, $amount) {      
    $player->transfer($amount);
}

:

transfer($amount) {
    if ($this->getWealth() < $amount) {
        throw NotEnoughMoney();
    }

    ...
}
+4
3

, :

//inside Behat context class method  
try {
  $this->outcome = $func();
}
catch(\Exception $ex) {
  $this->outcome = $ex;
}

, , , $this- > result /.

+2

, . , " "? ?

:

When <player> transfers <transfer> from his account
Then I should see error <error>

:

When <player> transfers <transfer> from his account
Then I should see "transfer successful"
0

This is how I successfully did this in my project, where I had to repeat several steps until the condition was confirmed:

/**
* @Given /^I execute some conditions$/
*/
public function executeConditions()
{
   $flag = 1;
    do {
        try {
            <steps to be executed till the condition holds true>
            $flag=1;
        } catch (\Exception $ex) {
            $flag = 0;
        }
    }while ($flag>0);
 }
0
source

Source: https://habr.com/ru/post/1525625/


All Articles