Jump to content

Recommended Posts

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 assert
statement, 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
Link to comment
https://forums.phpfreaks.com/topic/15658-php-unit-testing-questions/
Share on other sites

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]Why does the first one throw an error message, and not some assert
statement, 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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.