php - 扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的)

标签 php laravel phpunit

我希望有人能帮助我。我正在使用 laravel 4 并且我正在编写我的第一个单元测试一段时间但遇到了麻烦。我正在尝试扩展 TestCase 类,但出现以下错误:

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 上关注这个 Actor https://laracasts.com/lessons/tdd-by-example尽管您必须是订阅者才能观看视频,但文件位于视频下方,正如您所见,我所做的与 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);
    }
}

TestCase 类的开头:

<?php namespace Illuminate\Foundation\Testing;

use Illuminate\View\View;
use Illuminate\Auth\UserInterface;

abstract class TestCase extends \PHPUnit_Framework_TestCase {

顺便说一句——默认情况下,Laravel 测试类不会自动加载,所以我尝试了完全限定类名和使用 Illuminate\Foundation\Testing,然后只是扩展 TestCase。我知道它可以将其视为当我没有完全限定名称时它会提示找不到该类。我也试过:

composer dump-autoload

composer update

感谢任何帮助

最佳答案

根据你的错误信息:Class registrationTest contains 1 abstract method Base Class contains an abstract method 并且当 Child Class 使用抽象方法扩展另一个类,然后子类应实现 Base class 中可用的抽象方法。所以,registrationTest 是子类,\Illuminate\Foundation\Testing\TestCase 是基类,它包含一个抽象方法:

\Illuminate\Foundation\Testing\TestCase中的一个抽象方法:

abstract public function createApplication();

因此,在您的子类/registrationTest 中,您必须实现此方法:

public function createApplication(){
    //...
}

但是,实际上您不需要直接扩展 \Illuminate\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 类作为基类来创建测试用例。因此,在 app/tests 文件夹中创建您的测试并创建如下类:

class registrationTest extends TestCase {
    public function testBasicExample()
    {
        $crawler = $this->client->request('GET', '/');
        $this->assertTrue($this->client->getResponse()->isOk());
    }
}

阅读Class Abstraction .

关于php - 扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23450867/

相关文章:

php - WordPress 类别小部件下拉插件

javascript - 插入外部页面后更改所有href

php - 如何使用 orWhere 创建动态 where 子句 - Laravel 4

PHPUnit 覆盖 : Allowed memory size of 536870912 bytes exhausted

php - Laravel 测试 phpunit 按 NotFoundHttpException

php - 如何使用 PEAR 安装程序完全删除 PHPUnit?

javascript - 为什么ajax请求时字符 "+"没有发送到php文件?

php - 仅在一个站点上显示 PHP 错误

php - generated_conversions 中的 Laravel 媒体库错误

json - 从命令行界面将命名空间添加到composer.json文件中的PSR-4-Section