gannina Posted July 26, 2006 Share Posted July 26, 2006 Hi, I am trying to learn how to do unit testing with Simpletest, but there are a few things I don't get.1. Do you always make your methods return (true, false, or a value)?- if a method doesn't return anything is there another way to test whats going on in a method or is it standard practice to just make you methods return something?2. I was reading through a book and it had the following example[code]If (strlen ($pass) < 5 ) { throw new Exception("Password must have 5 or more letters");}If (is_null ($name) { return false;}[/code]Why does the first one throw an error message, and not some assertstatement, or both?The second statement returns false, so it can be tested in a test case. Why not just display an error message instead?Like: [code]echo 'Name is empty';[/code]I'm kinda confused on those issues. Also, can anyone recommend a simple introduction to unit testing. The documentation for simpletest doesn't cover the basics in unit testing. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/15658-php-unit-testing-questions/ Share on other sites More sharing options...
redarrow Posted July 26, 2006 Share Posted July 26, 2006 Take a look at this and work it out why it does what it does then your understand valadation a little better ok Good luck.<?php$word="my name is redarrow and i love php";$number_of_charecters=strlen($word);if($word > 50) {echo " my good that a long word";}elseif($word > 100 ) {echo " you word is kinda all right";}elseif($word <= 34) {echo " <br> i love you sentence <br> numbe of chareters was$number_of_charecters <br> $word <br>";}?> Quote Link to comment https://forums.phpfreaks.com/topic/15658-php-unit-testing-questions/#findComment-63836 Share on other sites More sharing options...
gannina Posted July 26, 2006 Author Share Posted July 26, 2006 I guess the user doesn't want to see those error messages? Quote Link to comment https://forums.phpfreaks.com/topic/15658-php-unit-testing-questions/#findComment-64088 Share on other sites More sharing options...
trq Posted July 26, 2006 Share Posted July 26, 2006 [quote]Why does the first one throw an error message, and not some assertstatement, or both?[/quote]Exceptions can be caught. That is there point.[code=php:0]function foo($a=false) { if (!$a) { throw new exception('your argument is false'); } else { return true; }}try { foo();} catch (exception $e) { echo $e->getmessage(); exit();}echo "your argument passed to foo was true";[/code]I think the reason the example you posted is how it is is it lets you differentiate between the two different types of failures. ie; If the users pass is too short it will throw an exception, if there username is null it will return false, if neither, then you have a valid combination.Its probably not a good example of exception handling. exceptions are a funny thing, Its hard to understand when to use them. You can... if you wish... use them at every condition, however they become messy.IMO, its best to use exceptions when the application relies on the effected action to work. ie; your database connection. Exception let you try different approuches to different errors, then hopefully fail gracefully. For general stuff (data validation and the like), I prefer to just have my functins return true / false.Really, its up to you though. Hope that helps... probably confused the issue even further :-P Quote Link to comment https://forums.phpfreaks.com/topic/15658-php-unit-testing-questions/#findComment-64118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.