laravel 在 Codeception Functional Cept 中设置预期异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24984911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:52:14  来源:igfitidea点击:

Set Expected Exception in Codeception Functional Cept

phplaravellaravel-4phpunitcodeception

提问by Martin

I want to do something like:

我想做类似的事情:

$I->setExpectedException('Laracasts\Validation\FormValidationException');

In a functional cept. Any chance to do so?

在功能概念中。有机会这样做吗?

\PHPUnit_Framework_TestCase::setExpectedException('Laracasts\Validation\FormValidationException');

Above code will work in isolation but if I run codecept run, the tests get stuck once the test with the expected exception is complete.

上面的代码将单独工作,但如果我运行codecept run,一旦具有预期异常的测试完成,测试就会卡住。

Here's my setup:

这是我的设置:

YML:

YML:

class_name: FunctionalTester
modules:
    enabled: [Filesystem, Db, FunctionalHelper, Laravel4, Asserts]

回答by Mind

I think this is a known problemwith the Laravel 4 module for codeception, not sure if it is going to be fixed soon, but in the meantime I created a helper function to test Exceptions:

我认为这是用于代码接收的 Laravel 4 模块的一个已知问题,不确定它是否会很快修复,但同时我创建了一个辅助函数来测试异常:

In the file tests/_support/FunctionalHelper.phpadd the following method:

在文件tests/_support/FunctionalHelper.php 中添加以下方法:

public function seeExceptionThrown($exception, $function)
{
    try
    {
        $function();
        return false;
    } catch (Exception $e) {
        if( get_class($e) == $exception ){
            return true;
        }
        return false;
    }
}

You use it in your Ceptslike this:

您可以像这样在Cepts 中使用它:

$I = new FunctionalTester($scenario);
$I->assertTrue(
    $I->seeExceptionThrown('Laracasts\Validation\FormValidationException', function() use ($I){
    //All actions that you expect to generate the Exception
    $I->amOnPage('/users/edit/1');
    $I->fillField('name', '');
    $I->click('Update');
}));

回答by Oliver Schupp

I extended Minds solution a bit:

我稍微扩展了Mind的解决方案:

public function seeException($callback, $exception = 'Exception', $message = '')
  {
    $function = function () use ($callback, $exception) {
      try {
        $callback();
        return false;
      }
      catch (Exception $e) {
        if (get_class($e) == $exception or get_parent_class($e) == $exception) {
          return true;
        }
        return false;
      }
    };
    $this->assertTrue($function(), $message);
  }

In your cepts you can

在你的概念中,你可以

$I->seeException('Laracasts\Validation\FormValidationException', function() use ($I){
    //All actions that you expect to generate the Exception
    $I->amOnPage('/users/edit/1');
    $I->fillField('name', '');
    $I->click('Update');
})

This way there is no need for assertTrue outside of the exception test method. You can also leave out the second argument if it is fine for you too know that an exception is thrown. Last you can put an message string as third argument. That way you can make the error more expressive if it fails.

这样就不需要在异常测试方法之外使用 assertTrue。如果您也知道抛出了异常,那么您也可以省略第二个参数。最后,您可以将消息字符串作为第三个参数。这样,如果失败,您可以使错误更具表现力。

EDITPardon, I missed one thing. In FunctionalHelper.php you have to extends the parents _beforeSuite method:

编辑对不起,我错过了一件事。在 FunctionalHelper.php 中,您必须扩展父 _beforeSuite 方法:

public function _beforeSuite()
  {
    parent::_beforeSuite();
    $this->assert = $this->getModule('Asserts');

  }

EDIT2Just found out that the test method doesn't work if you implicitly assert that an exception is thrown and php's native exception isn't the appearing exceptions parent because the method only checks first and second exception level. To make the method inspect the whole expection stack you have to use this method

EDIT2刚刚发现如果您隐式断言抛出异常并且 php 的本机异常不是出现的异常父级,则测试方法不起作用,因为该方法仅检查第一和第二异常级别。要使该方法检查整个期望堆栈,您必须使用此方法

public function assertThrows($callback, $exception = 'Exception', $message = '')
  {
    $function = function () use ($callback, $exception) {
      $getAncestors = function($e) {

        for ($classes[] = $e; $e = get_parent_class ($e); $classes[] = $e);
        return $classes;

      }

      try {
        $callback();
        return false;
      }
      catch (Exception $e) {
        if (get_class($e) == $exception or in_array($e, $getAncestors($e))) {
          return true;
        }
        return false;
      }
    };
    $this->assertTrue($function(), $message);
  }