Call to undefined method Tests\Unit\TopPageTest::get() in Unit test in Laravel 8

Viewed 2214

I am writing a code to make Unit test for each pages in my project and started with the Main page. The main page consists of shop lists, genre lists and area lists.

tests\Unit\ToppageTest.php :

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
// use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Shop;

class TopPageTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testIndex()
    {
        $response = $this->get('/');
        $this->assertContains('data', $response->content());
        $response->assertSuccessful();
    }
}

phpunit.xml:

 <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>

App\Http\Controllers\TopController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\ToppageService;

class TopController extends Controller
{
    private $shop;

    public function __construct(ToppageService $shop)
    {
        $this->shop = $shop;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = $this->shop->getAllServicer();
         return view('index', compact('data'));

    }

}

App\Http\Services\ToppageService.php:

<?php

namespace App\Services;

use App\Services\BaseService;
use App\Models\Shop;
use App\Models\Genre;
use App\Models\Area;

class ToppageService extends BaseService
{
    protected $shop, $genre, $area;

    public function __construct(Shop $shop, Genre $genre, Area $area)
    {
        $this->shop = $shop;
        $this->genre = $genre;
        $this->area = $area;
    }

    public function getAllServicer()
    {
        $data = [] ;
        $data['shoplist'] = $this->shop->with('attribute','content','coupon','menu_categoly','area','genre')->whereHas('servicer',function($query){$query->where('name', config('const.system.setting.servicer'));})->orderBy('created_at', 'DESC')->get();
        $data['genrelist'] = $this->genre->get();
        $data['arealist'] = $this->area->get();
        return $data;
    }
}

When I enter php artisan test or vendor/bin/phpunit in command prompt, I get an error like:

Call to undefined method Tests\Unit\TopPageTest::get()

  at E:\tulsi\wparijat\laravel\laravel-awapass\app.awapass.com\tests\Unit\ToppageTest.php:20
     16▕      */
     17▕     public function testIndex()
     18▕     {
     19▕         //not working
  ➜  20▕         $response = $this->get('/');
     21▕         $this->assertContains('data', $response->content());
     22▕         $response->assertSuccessful();
     23▕     }
     24▕ }

  1   E:\tulsi\wparijat\laravel\laravel-awapass\app.awapass.com\vendor\phpunit\phpunit\phpunit:76
      PHPUnit\TextUI\Command::main()


  Tests:  1 failed
  Time:   0.05s

when I add use Tests\TestCase; instead of use PHPUnit\Framework\TestCase;

I get error:

Warning: TTY mode is not supported on Windows platform.

I searched for solution for could't get the exact solution for this. Can anyone please help me?

2 Answers

You seem to be mixing syntax between whatever Laravel's TestCase is doing with PHPUnit's TestCase. If you're just using PHPUnit you probably need to do is something more like

public function testIndex()
{
    $mockService = $this->createMock(ToppageService::class);
    $controller = new TopController($mockService);
    
    $mockData = 'MOCK DATA';
    
    $mockService
        ->expects($this->once()
        ->method('getAllServicer')
        ->willReturn($mockData);
    
    $response = $controller->index();
    
    $this->assertContains('data', $response->content());
}

This is purely a Unit Test, not a Functional Test, so it's just mocking whatever the service is doing. It's probably also not exactly what you need; I've no idea what view or compact are doing, and you probably need to mock up something more accurate in your data.

uncomment this line:

// use Tests\TestCase;

and comment this one:

use PHPUnit\Framework\TestCase;
Related