Phpunit testing expectedException not working

I try to check my class for an InvalidArgumentException, but I get

Tests \ BarTest :: should_receive_parameter Missing argument 1 for Itdc \ Foo \ Bar :: __ construct (), called in /mypath/foo/tests/BarTest.php on line 10 and is defined

This is the test file (BarTest.php) that I use:

<?php namespace Tests;
use Itdc\Foo\Bar;
class BarTest extends \PHPUnit_Framework_TestCase {
    /** @test */
    public function should_receive_parameter() {
        $this->setExpectedException('Exception');
        $id = new Bar;
    }
}

This is the Bar class:

<?php namespace Itdc\Foo;
class Bar {
    public function __construct($a) {
        // do something
    }
}

I tried puth setExpectedException in the comments section, also tried to use an InvalidArgumentException, but no luck.

Any suggestions on what I am doing wrong will be appreciated.

+4
source share
1 answer

If you catch it directly, you can throw a var_dump () exception.

/** @test */
public function shouldThrowException() {
  try {
    new Foo();
  } catch (\Exception $e) {
    var_dump($e);
  }
}

Conclusion:

class PHPUnit_Framework_Error_Warning#19 (9) {
  ...

, , , PHPUnit . .

:

/** @test */
public function shouldThrowException() {
  $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  new Foo();
}
+3

All Articles