Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

PHPUnit Exception testing

Go to End

2 Posts   1243 Views


Community Member, 254 Posts

19 September 2013 at 1:00pm

HI all,

I'm having trouble verifying exceptions.

* @expectedException ValidationException
public function test_project_should_not_save_without_expiry() {
$project = new Project();

Inside project validate function I'm checking !isset( $this->Expiry ) which stops the writing of a Project. But test results I get "Failed asserting that exception of type "ValidationException" is thrown."

The code works if I set @expectedException Exception and then throw new Exception(); inside the test.

I also tried $this->assertFalse( $project->write() ); after looking at DataObject:1065 it looks like that should work.

Any thoughts?


Community Member, 254 Posts

24 September 2013 at 12:20pm

OK so it was all working as it should. Trouble was in my validate function I was checking isset. Which it was set with an empty string. So switched to !trim($this->Expiry) and bingo! successfully asserting validation exception.