我在CalTest类的Cal类中有div()有以下方法来测试div()。
public fucnction div($a,$b){
if($b == 0){
throw new Exception("Divided by zero");
}
return $a/$b
}
我只能传递testDiv()但是传递testDiv2()。
我想要检查使用PHPUnit正确抛出的exeption。 我在这里失踪了什么? 非常感谢您的帮助。 谢谢!
可能重复的PHPUnit断言抛出了异常?
嗯,看起来你的注释是正确的,实际上(也许不是重复的)。 您是否尝试删除注释@和异常名称之间的选项卡? 编辑:实际上,我认为问题是您的expectedMessage不是异常消息。
@Corbin我读了那个问题并试了一下,尝试了不同的例外。 但没有结果
PHPUnit会自动捕获异常吗? 你不必做一个try / catch块吗?
@Anthony不,你在RJ的评论栏中这样做了。
@vascowhite - 你能告诉我他们在哪里捕获错误,因为我只看到被调用的方法并抛出异常,没有捕获的迹象。 所以我想知道我是否是盲人或是否有我不知道的try / catch语法(并且可能想开始使用)。
@Anthony在这里看到phpunit.de/manual/3.7/en/…
你的第二个截图(有错误的截图)有
"@expectedException Exception"
而第三个
@expectedException InvalidArgumentException
你还真的得到错误吗?你保存文件了吗?
适合我:
Foo.php
class Foo
{
static function t()
{
throw new InvalidArgumentException('Hi there');
}
}
?>
FooTest.php
require_once 'Foo.php';
class FooTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function testT()
{
Foo::t();
}
}
?>
结果
$ phpunit .
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 5.25Mb
OK (1 test, 1 assertion)
是的,这是真的。那是因为我在拍摄屏幕时尝试了两种方式(两种方法都不起作用)。对不起。
你的功能完美无缺。但在我的情况下,它仍然说invalidAargumetExepetion
然后请发布重现问题的最小代码。我确定问题就在你身边。
是的问题在我身边。只有testDiv2()让我感到麻烦,而不是另一个,所以我猜这是我问题的最小代码。
不,我的意思是我可以复制和粘贴的代码,以便在我的机器上重现您的问题。
所以,任何想法应该如何写?
您仍然没有提供任何我可以复制和粘贴的代码来重现此处的错误。
刚刚遇到同样的问题。由于某种原因,PHPUnit不允许您将expectedException设置为一般异常,我不知道为什么。我个人选择抛出自定义的异常代码,而不是每次我想区分异常时都要创建一个新的异常类。
以下是我如何解决它:
/**
* @expectedException Test_Exception
*/
public function testDivByZero()
{
try {
// Fyi you don't need to do an assert test here, as we are only testing the exception, so just make the call
$result = $this->object->div(1,0);
} catch (Exception $e) {
if ('Exception' === get_class($e)) {
throw new Test_Exception($e->getMessage(), $e->getCode());
}
}
}
// Test_Exception.php
class Test_Exception extends Exception
{
public function __construct($message = null, $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
这将允许您按照自己的方式设计代码,并抛出"通用"异常。基本上它只测试Exception类,如果它是通用的,则将其重新包装为另一个异常; Test_Exception。
- 更新 -
昨天发现他们已经删除了当前"主"分支中的通用异常限制,该分支将为3.7。显然,首席工程师不想修补3.6。
此问题已在PHPUnit 3.7.x中修复
您可以在PHPUnit 3.7.x中使用以下通用异常
/**
* @expectedException Exception
*/
它在PHPUnit抛出的异常中如此表示:
You must not except the generic exception class.
让您的类抛出更详细的异常,并进一步指定您在单元测试中期望的异常类型。
即使我期望InvalidArgumentException也无法正常工作。我认为这不是通用的异常类,对吧?
虽然不是你,但你正在抛出一般的异常?
@vascowhite:我该怎么办呢?那么我会抛出InvalidArgumentException吗?
如果这就是你告诉PHPUnit所期望的那样,那么你应该抛出的逻辑是不是它?
这是一个可怕的建议。拥有供应商API决定如何设计代码是一种不好的做法。 IMO PHPUnit放弃了这个。我们中的一些人选择异常代码而不是抛出自定义异常。
我创建了一个父类方法来完成它。事实上,这门课程来自laravel,但在任何情况下都有效。
这个方法很酷的部分是在PHP中使用匿名函数
class TestCase
/* if you are not using laravel, you dont need this
extends Illuminate\Foundation\Testing\TestCase */ {
protected function assertThrows( $function , $classException ="/Exception" )
{
try
{
// Anonymous functions FTW
$function();
}
catch( /Exception $objException )
{
// the assertInstanceOf should solve from here but
// it is not working that great
// @see http://stackoverflow.com/questions/16833923/phpunit-assertinstanceof-not-working
if( $objException instanceof $classException ) {
return $this->assertTrue( true );
} else {
// this will throw a error with a cool message
return $this->assertEquals( $classException , get_class( $objException ));
}
}
// no exception happened.
return $this->fail("Exception" . $classException ." expected." );
}
}
class LanguageTest extends TestCase {
/**
* Test the translation
*
* @return void
*/
public function testTranslation()
{
// this test may be ok
$this->assertThrows(
function(){ throw new Full\Path\Exception(":("); },
"Full\Path\Exception" );
// this test may fail
$this->assertThrows(
function(){ return 1 + 1; },
"Some\Excepted\Exception" );
// this test may work
$this->assertThrows(
function(){ throw new Exception("sad" ); }
);
}
}
你可以做这样的事情
编写单元测试,然后注入触发异常所需的值
//断言
$这 - > assertTrue($这 - > setExpectedException( 'PHPUnit_Framework_ExpectationFailedException'));
我不清楚你说的是什么,你是否告诉我将这些代码插入testDiv()或其他东西?我试过了你,但你仍然是相同的:'(
我不明白你的评论,但我建议使用phpunit资源。 phpunit.de/manual/3.2/en/…