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