digitalhuman
3 years ago
2268
5
Testing

Laravel 5.4 PHP Unit Test missing methods and not working

Posted 3 years ago by digitalhuman

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", "[email protected]");

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

$this->call("GET", "[email protected]")............. 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());
        
    }
}

Please sign in or create an account to participate in this conversation.