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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   1554 Views

Avatar
Bambii7

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();
$project->write();
}

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?

Avatar
Bambii7

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.