【问题标题】:Call to undefined method Tests\Unit\TopPageTest::get() in Unit test in Laravel 8在 Laravel 8 的单元测试中调用未定义的方法 Tests\Unit\TopPageTest::get()
【发布时间】:2021-09-16 20:16:14
【问题描述】:

我正在编写代码来对项目中的每个页面进行单元测试,并从主页开始。主页面由店铺列表、流派列表和区域列表组成。

测试\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;
    }
}

当我在命令提示符下输入 php artisan test 或 vendor/bin/phpunit 时,出现如下错误:

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

当我添加使用 Tests\TestCase;而不是使用 PHPUnit\Framework\TestCase;

我得到错误:

Warning: TTY mode is not supported on Windows platform.

我搜索了解决方案,但无法获得确切的解决方案。谁能帮帮我?

【问题讨论】:

  • $this-&gt;get('/');是什么,get函数在哪里?
  • 它在 routes/web.php 中作为 Route::get('/', 'App\Http\Controllers\TopController@index');
  • 可能不是一个严重的错误,而只是一个警告。如果您不在 Windows 下运行测试套件,我很确定它会消失。你试过吗?否则也许不值得担心。
  • 对于致命错误,请检查 $this-&gt;get('/'); 。在您当前的配置中,它不起作用。可能是设置您的测试套件时出错,可能不是 Phpunit,而是 Laravell 的扩展(可能缺少?)。

标签: php unit-testing phpunit laravel-8 laravel-testing


【解决方案1】:

您似乎在混合 Laravel 的 TestCase 与 PHPUnit 的 TestCase 之间的语法。如果你只是使用 PHPUnit,你可能需要做的更像是

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());
}

这纯粹是一个单元测试,而不是功能测试,所以它只是在模拟服务正在做的任何事情。它可能也不完全是您所需要的;我不知道viewcompact 在做什么,你可能需要在data 中模拟一些更准确的东西。

【讨论】:

  • 它给出了一个错误:方法 Illuminate\View\View::content 不存在。我们不需要为此创建工厂吗?视图正在返回索引刀片,其中包含由商店、流派和区域列表组成的 $data。
  • @TulsiAcharya:这是示例代码。当您只是复制和粘贴它时,您可能会看到错误,因为它不是为此而设计的。您阅读它,尝试了解它的作用和原因(因此是示例),如果其中有某些内容,您可以相应地调整自己的代码或开始测试以更深入地研究该方向。
【解决方案2】:

取消注释这一行:

// use Tests\TestCase;

并评论这个:

use PHPUnit\Framework\TestCase;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 2018-03-22
    • 2018-03-05
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多