Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to test that function throws an exception in pest

Writing a test to expect a test to throw an exception can be done as follows:

it('throws exception', function () {
    throw new Exception('Something happened.');
})->throws(Exception::class);

    
If you wish to assert the exception message too, you need to give a second argument to the throws method:

it('throws exception', function () {
    throw new Exception('Something happened.');
})->throws(Exception::class, 'Something happened.');

    
If you're only interested in the message, and the exception type isn't important, you can just provide the message by itself:

it('throws exception', function () {
    throw new Exception('Something happened.');
})->throws('Something happened.');

    
You may also use the throwsIf method to conditional assert an exception if a given boolean expression evaluates to true:

it('throwsIf exception', function () {
    // ..
})->throwsIf(fn() => DB::getDriverName() === 'mysql', Exception::class, 'MySQL is not supported.');

    
You may also assert one or more exceptions inside your test function with Expectations' method toThrow()
Comment

PREVIOUS NEXT
Code Example
Php :: wp override home url with php 
Php :: php replace method that takes infinite input 
Php :: php counting number of chars excluding newlines 
Php :: php date format 
Php :: php artisan serve 
Php :: php number format 2 decimal no comma 
Php :: php json_decode 
Php :: ternary operator laravel blade 
Php :: How to display data from MySQL database into HTML table using PHP 
Php :: how change the languge of fie manager in laravel 
Php :: send sms by php diafaan 
Php :: laravel migrate only one table 
Php :: add a new column to existing table in a migration 
Php :: php get intersection of two arrays 
Php :: php sql last 10 rows 
Php :: memory limit wordpress 
Php :: replace all php 
Php :: select sum in laravel 
Php :: laravel descending order paginate eloquent 
Php :: how to set session in laravel 
Php :: Undefined property: CollectiveAnnotationsRoutingAnnotationsResourcePath::$no_prefix 
Php :: php carbon convert string to date 
Php :: install ext-ldap php 7.2 
Php :: php check weekday of date 
Php :: php get last index end in foreach 
Php :: laravel model increase the value by one 
Php :: webstorm vs phpstorm 
Php :: limit offset array php 
Php :: foreach loop in php 
Php :: implode php 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =