How to resolve undefined-method error in Laravel's PHPUnit testing
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
I have summarized the solution for when an "undefined-method" error occurs while testing with PHPUnit in Laravel.
Error Message
There was 1 error:
1) Tests\Unit\PostItemTest::testgetPosts
Error: Call to undefined method Tests\Unit\PostItemTest::get()
Solution
The error message indicates that the function cannot be found.
While specific to PHPunit, this error occurs when the naming conventions are not followed. Please try making the following changes:
Starting the function name with test_
public function test_getPosts()
{
}
Adding @test annotation
/**
*
* @test
* @return void
*/
public function getPosts()
{
}
By writing it like this, the "function not found" error should be resolved.
Tips
You can write function names in Japanese.
Writing in Japanese can make it easier to understand when tests don't pass.
public function test_どこのなんのテスト()
{
}
Conclusion
That's all for now.
I hope this can be helpful for someone.
For feedback or complaints, please contact me via Twitter DM.
Thank you!
Popular Articles
Deploying PHP7.4 + Laravel6 Project to AWS EC2
Implementing Breadcrumbs in Laravel with laravel-breadcrumbs