Jump to:

23374 Posts in 18175 Topics by 2866 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: 320 Views
  • Bambii7
    Avatar
    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();
          $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?

  • Bambii7
    Avatar
    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.

    320 Views
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.