Php 扩展Laravel'时出错;s抽象测试用例类(错误是我的扩展类必须是抽象的)
我希望有人能帮我。我正在使用Laravel4,我正在编写我的第一个单元测试,但我遇到了麻烦。我试图扩展TestCase类,但出现以下错误:Php 扩展Laravel'时出错;s抽象测试用例类(错误是我的扩展类必须是抽象的),php,laravel,phpunit,Php,Laravel,Phpunit,我希望有人能帮我。我正在使用Laravel4,我正在编写我的第一个单元测试,但我遇到了麻烦。我试图扩展TestCase类,但出现以下错误: PHP Fatal error: Class registrationTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Foundation\Testing\TestCa
PHP Fatal error: Class registrationTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Foundation\Testing\TestCase::createApplication) in /home/john/www/projects/MyPainChart.com/app/tests/registrationTest.php on line 4
如果我有这个权利,那么错误是指一个方法是抽象的,那么它所在的类也必须是抽象的。从TestCase类下面可以看到,它是抽象的。我搜索了这个错误,但画了一个空白
试着在Laracasts上跟随这个演员,虽然你必须是一个订户才能观看视频,但文件就在它下面,正如你所看到的,我对Jeffrey Way没有什么不同
我的测试:
<?php
class registrationTests extends \Illuminate\Foundation\Testing\TestCase
{
/**
* Make sure the registration page loads
* @test
*/
public function make_sure_the_registration_page_loads_ok()
{
$this->assertTrue(true);
}
}
及
根据您的错误消息:
类注册测试包含1个抽象方法
基类包含一个抽象方法
,当一个子类
使用抽象方法扩展另一个类时,子类应实现中可用的抽象方法基类
。因此,registrationTest
是子类,\Lightning\Foundation\Testing\TestCase
是基类,它包含一个抽象方法:
\illumb\Foundation\Testing\TestCase
中的抽象方法:
因此,在子类/注册测试中
必须实现此方法:
public function createApplication(){
//...
}
但是,实际上您不需要直接扩展\illumb\Foundation\Testing\TestCase
,因为在app/tests
文件夹中有一个类TestCase/TestCase.php
,您可以扩展这个类:
// In app/tests folder
class registrationTest extends TestCase {
//...
}
TestCase.php
类如下所示:
class TestCase extends Illuminate\Foundation\Testing\TestCase {
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
}
注意,TestCase
已经实现了这个抽象方法createApplication
,所以您不需要在类中扩展它。您应该使用TestCase
class作为基类来创建测试用例。因此,在app/tests
文件夹中创建测试,并创建如下类:
class registrationTest extends TestCase {
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
阅读。首先进入composer.json并添加
"scripts" : {"test" : "vendor/bin/phpunit"
}
然后运行composer update
然后
path/test/中的TestCase.php类应该如下所示
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase {
use CreatesApplication;
}
只需在你的班级中排名靠前,如下所示,你就可以走了
<?php
namespace YOUR_CLASS_PATH;
use Tests\TestCase;
class UserTest extends TestCase{
...//your business logic here
}
谢谢-你是个明星。今天早些时候,我错误地认为PHPSpec是PHPunit的替代品,不能与PHPunit一起使用(我现在知道了真相),但我匆忙删除了tests dir,因此删除了TestCase.php文件。我还从composer.json中删除了该文件。只有当我意识到我需要两者时,我才重新创建了dir。多亏了git,一切又好了。再次感谢
class registrationTest extends TestCase {
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
"scripts" : {"test" : "vendor/bin/phpunit"
}
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase {
use CreatesApplication;
}
<?php
namespace Tests\Feature;
use Tests\TestCase;
class registrationTests extends TestCase {}
<?php
namespace YOUR_CLASS_PATH;
use Tests\TestCase;
class UserTest extends TestCase{
...//your business logic here
}