digitalhuman's avatar

Laravel 5.4 PHP Unit Test missing methods and not working

Hi all,

I have been googling for a while now end stranded everytime. I use Laravel 5.4. What I try to do is to get a basic UnitTest (the build in artisan test) to work.

I have 1 JSON endpoint which I would love to test. Not 1 example that I found online works. I either mis something in my setup or I completely lost it.

I can post here 10000 examples, but I will keep it clean and simple; What I want is:

Test a method from my controller. As examples show that sould be possible with:

$this->action("GET", "MyController@getMethod");

Problem is, I always het the error "Method action is not defined". So, I tried another option. The call method.

$this->call("GET", "MyController@getMethod")............. meh not working either. Oh well, wtf, lets try: $this->call("GET", "api.get.something").... Well it does something, but after reading +/- 238472341641294823 lines of error message I discovered that he can't find this route. So, the last option:

$client = new \GuzzleHttp\Client();
$response = $client->get("http://127.0.0.1/api/getsomething");

I don't want to use this because these test can't rely on 127.. but anyway. Lets try:

$this->assertViewHas()..... method not found. Fuck it; $this->assertJson($response->getBody()->getContents())..... AAAAAAAAAaaaaaaaaaaa finally. Like I sad. Totally useless test because of 127.

Yes to all below responses;

in ./tests/ is have TestCase.php, CreatesApplication.php is used php artisan:test JustATestCase --unit

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class MapTest extends TestCase
{
    public function setUp() {
        parent::setUp();
    }
    
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testGetEventData()
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->get("http://127.0.0.1/api/map/events");
        
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertJson($response->getBody()->getContents());
        
    }
}
0 likes
5 replies
undefined's avatar

Hi DIGITALHUMAN,

It's beed 9 months, but whatever... I use laravel 5.5 and have exactly the same problem. When trying to test controller i.e. $this->action('GET', 'ExampleController@index'); I get Error: Call to undefined method ...action() Any solutions for that?

WTF laravel team!? It's one of the first examples from docs and it's not working! I'm starting to understand why there is so much hate on this framework...

tykus's avatar

It's one of the first examples from docs

Do you have a link for that example?

nicolanilomartino's avatar

It is now 2020, tests are still broken. Laravel still ships this broken part right out of the box for beginners to be frustrated right of the bat. They'd be up for a perfect surprise when the very first example on the docs doesn't work.

all methods for testing will be "Method undefined" except for assertTrue in the exampleTest. It is as good as "return true" but nothing was really tested. waste of time.

_Marco_'s avatar

This happens to me in some assertions as well like assertMatchesRegularExpression() just throws an error:

Error: Call to undefined method

I am extending TestCase from Tests\TestCase which is extending BaseTestCase from Illuminate\Foundation\Testing\TestCase.

Looks like something that should be fixed.

I am using Laravel 6 btw.

Please or to participate in this conversation.