【问题标题】:Error extending Laravel's abstract TestCase class (error is saying that my extended class must be abstract)扩展 Laravel 的抽象 TestCase 类时出错(错误是说我的扩展类必须是抽象的)
【发布时间】:2014-06-20 11:30:30
【问题描述】:

我希望有人可以帮助我。我正在使用 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 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

任何帮助表示赞赏

【问题讨论】:

    标签: php laravel phpunit


    【解决方案1】:

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

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

    abstract public function createApplication();
    

    所以,在你的child class/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

    【讨论】:

    • 谢谢 - 你是明星。今天早些时候,我错误地认为 PHPSpec 是 PHPunit 的替代品,而不是一起使用(我现在知道真相),但我匆忙删除了测试目录,因此删除了 TestCase.php 文件。我还从 composer.json 中删除了该文件。只有当我意识到我需要两者时,我才重新创建了目录。感谢 git 一切都好起来了。再次感谢
    【解决方案2】:

    首先进入composer.json并添加

    "scripts" : {"test" : "vendor/bin/phpunit"
        }
    

    然后运行 ​​composer update 那么

    路径 /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;
    }
    

    那么你的 registrationTests 类应该是这样的

    <?php
    
    namespace Tests\Feature;
    
    use Tests\TestCase;
    
    class registrationTests extends TestCase {}
    

    【讨论】:

      【解决方案3】:

      只需按以下方式在您的班级排名靠前,您就可以开始了,

      <?php
      
      namespace YOUR_CLASS_PATH;
      
      use Tests\TestCase;
      
      class UserTest extends TestCase{
           ...//your business logic here
      }
      

      我希望这行得通。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-16
        • 2016-11-10
        • 2021-08-20
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多