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, biapar, Willr, Ingo, swaiba, simon_w

PHPUnit Exception testing


2 Posts   362 Views


19 September 2013 at 1:00pm Community Member, 254 Posts

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?


24 September 2013 at 12:20pm Community Member, 254 Posts

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.