I’m just learning unit testing. This php code
class Foo {
public function bar($arg) {
throw new InvalidArgumentException();
}
}
…
class FooTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$this->setExpectedException('InvalidArgumentException');
$dummy = Foo::bar();
}
}
fails with Failed asserting that exception of type "PHPUnit_Framework_Error_Warning" matches expected exception "InvalidArgumentException". from phpunit. If any value is placed within the Foo::bar() test then it, of course, works as expected. Is there a way to test for empty arguments? Or am I erroneously trying to create a test for something that shouldn’t be within the scope of a unit test?
You shouldn’t test such situations. A purpose of a unit test is to make sure that a class under test performs according to its “contract”, which is its public interface (functions and properties). What you’re trying to do is to break the contract. It’s out of scope of a unit test, as you said.