0

I am working on cakephp. I want to know how can I test if something is not saving into db. I mean I want to do a unit testing and I want to echo the result just for checking purposes and to know whether I am writing a correct exception handling code or not. here is my code which I am trying

try {
    if (! $this->User->save($data)) 
        throw new Exception('Error saving data'); 
} catch (Exception $e) { 
    echo "data is not saving into the db";
    die();
}

echo "data saves successfully";

is this the right code above.and what result will printed out on the screen if data doesn't successfully save into DB. THIS one "Error saving data" or this one "data is not saving into the db"

1

1 Answer 1

0

Nothing in your code looks like an unit test at all. You don't echo "test results" you "assert" and "expect" them. Have you tried reading the manual before starting to write your "test" code?

See this answer, it explains how to test an exception: How to unit testing Exceptions with PHPUnit?

2
  • sorry I don't want to do unit testing. Actually I am writing an Api so I want to return the result to my mobile app if something doesn't save successfully. so I Want to know whether I wrote a right code for that or not ? or should i simply check condition with true of false like issaved. I mean should I have to prefer try catch or not ? Commented Mar 9, 2015 at 6:23
  • See this page then instead book.cakephp.org/2.0/en/views/json-and-xml-views.html And a proper designed API is a lot more than just echoing true or false.
    – floriank
    Commented Mar 9, 2015 at 8:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.