我正在尝试使用PHPUnit在Laravel中编写我的第一个单元测试。我正在编写的测试试图验证一个相当简单的helper函数是否正常工作。编写它的方式是,我的函数需要一个或两个参数,然后尝试验证参数是否合适;换句话说,如果函数在为参数提供的特定值上抛出InvalidArgumentException,那么函数就可以正常工作。
如何编写一个单元测试,实际上,如果函数在第二个参数中得到一个十进制数,而它需要的是一个int(或者提供了一个int,但它超出了范围),则应该抛出InvalidArgumentException?Assert应该是什么样子的?(我看了可用Assert的列表,但没有任何东西适合异常。)
以下是我的全部职能:
namespace App\Helpers;
/**
* This class contains a variety of helper functions useful to this app.
*/
class Helper2
{
/**
* This function chooses a random number of elements from an array and chooses
* them at random.
*
* $array - the array of items from which elements will be selected
* $numberOfElements - the number of elements from the array that the user desires.
*
* The array needs to be a simple array of strings, integers, etc.
*
* The $numberOfElements defaults to null. When the value is null, the function
* will choose a random number of elements between 1 and the number of elements
* in the array. If a non-null value greater than 0 is provided, that number of
* elements will be returned by the function. If the number is larger than the
* number of elements in the array, all elements of the array will be returned.
*/
public static function choose_random_elements(array $array, int $numberOfElementsDesired = null)
{
// The first argument of the function must be an array. (No test necessary:
// Laravel will not even let you code the function call with anything but
// an array in the first argument.)
/* if (! is_array($array)) {
throw new \InvalidArgumentException("The input array is not actually an array.");
}
*/
// Store the array in a collection.
$myCollection = collect($array);
// The second argument of the function must be numeric.
if (! is_numeric($numberOfElementsDesired)) {
throw new \InvalidArgumentException ("The number of elements desired must be a number.");
}
// The second argument of the function must be an integer.
if (! is_integer($numberOfElementsDesired)) {
throw new \InvalidArgumentException ("The number of elements desired must be an integer.");
}
// The second argument of the function cannot exceed the number of elements in the array.
if ($numberOfElementsDesired > count($myCollection)) {
throw new \InvalidArgumentException ("The number of elements desired cannot exceed the number of elements in the array.");
}
// The second argument of the function cannot be zero or less.
if ($numberOfElementsDesired <= 0) {
throw new \InvalidArgumentException("You cannot choose a negative number of elements from the array.");
}
// If no value was supplied for the second argument of the function, choose
// an integer between 1 and the number of elements in the array.
if (is_null($numberOfElementsDesired)) {
$numberOfElementsDesired = rand(1, count($myCollection));
}
// If the number supplied for the second argument of the function exceeds
// the number of elements in the array, set the second argument to the size
// of the array.
if ($numberOfElementsDesired > count($myCollection)) {
$numberOfElementsDesired = count($myCollection);
}
// Choose a random number of elements at random from the collection and put them in a new collection.
$randomSelectionsCollection = $myCollection->random($numberOfElementsDesired);
// Convert the resulting collection into an array.
$randomSelectionsArray = $randomSelectionsCollection->toArray();
// Convert the array of selected elements into a string.
$randomSelectionsString = implode(',', $randomSelectionsArray);
echo "Function output: " . $randomSelectionsString . "\n";
return $randomSelectionsString;
}
}
我能想到的唯一替代方法是在try/catch块中编写测试。我想到了这个方法,而且看起来很有效。这是编写这种测试的合理方法还是有更好的方法?
try {
$result = $helper2->choose_random_elements(array(1, 6, "cat"), -4);
} catch(InvalidArgumentException $excp) {
$this->assertEquals("You cannot choose a negative number of elements from the array.", $excp->getMessage());
}
我正在运行拉勒维9号。
1条答案
按热度按时间dfddblmv1#
这是非常简单的事实,你的测试应该看起来像下面: