Jump to:

23453 Posts in 18902 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » PHPUnit Exception testing

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

Page: 1
Go to End
Author Topic: 343 Views
  • Bambii7
    Community Member
    254 Posts

    PHPUnit Exception testing Link to this post

    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?

  • Bambii7
    Community Member
    254 Posts

    Re: PHPUnit Exception testing Link to this post

    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.

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.