簡體   English   中英

Laravel單元測試:運行多個測試方法時重新定義錯誤

[英]Laravel unit tests: redefinition error when running multiple test methods

我想讀一個例子后運行一組單元測試這里需要一個活動的數據庫連接。 它似乎適用於一個單元測試,但由於重新定義了Laravel Command類中包含的常量,它將因多次測試而失敗。 以下是運行PHP單元后收到的錯誤:

PHPUnit 3.7.31 by Sebastian Bergmann.

FEE

Time: 203 ms, Memory: 5.00Mb

There were 2 errors:

1) MessageControllerTest::testSixMonths
ErrorException: Constant SHARE_SEND_EXPIRATION already defined

src/app/commands/DeleteExpiredSends.php:31
src/app/start/artisan.php:14
src/vendor/laravel/framework/src/Illuminate/Console/start.php:57
src/vendor/laravel/framework/src/Illuminate/Console/Application.php:30
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:70
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:45
src/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:206
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:21

2) MessageControllerTest::testOneYear
ErrorException: Constant SHARE_SEND_EXPIRATION already defined

src/app/commands/DeleteExpiredSends.php:31
src/app/start/artisan.php:14
src/vendor/laravel/framework/src/Illuminate/Console/start.php:57
src/vendor/laravel/framework/src/Illuminate/Console/Application.php:30
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:70
src/vendor/laravel/framework/src/Illuminate/Foundation/Artisan.php:45
src/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:206
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:46
src/app/tests/MessageControllerTest.php:21

FAILURES!
Tests: 3, Assertions: 1, Errors: 2.

這是我的單元測試:

<?php

class MessageControllerTest extends TestCase
{
    /**
     * setUp() : set up the unit tests by initializing the database
     */
    public function setUp()
    {
        // call the parent's setup method
        parent::setUp();

        // init the DB
        $this->migrateDatabase();
    }

    /**
     * createApplication() : bootstrap the app in order to execute
     * tests
     *
     * @return Symfony\Component\HttpKernel\HttpKernelInterface  
     */
    public function createApplication()
    {
        // set the unitTesting flag to true
        $unitTesting = true;

        // set the environment variable to 'testing'
        $testEnvironment = 'testing';

        return require __DIR__.'/../../bootstrap/start.php';
    }

    /**
     * migrateDatabase() : load the sqlite database into memory
     */
    private function migrateDatabase()
    {
        Artisan::call('migrate');
    }

    /**
     * testHundredFilesMessage() : tests to see if custom message will
     * return after 100 messages have been sent by the same user
     */
    public function testHundredFilesMessage()
    {
        // TODO
        $this->assertTrue(false);
    }

    /**
     * testSixMonthMessage() : test the six months message
     */
    public function testSixMonthMessage()
    {
        // TODO
        $this->assertTrue(false);
    }

    /**
     * testOneYearMessage() : test the one-year message
     */
    public function testOneYearMessage()
    {
        // TODO
        $this->assertTrue(false);
    }
}

第一個測試似乎通過,但所有后續測試都會拋出錯誤。 我猜不知怎樣多次調用createApplication()方法,這可能解釋為什么CommandDeleteExpiredSends被重新定義; 該類在其構造函數中顯示一個define語句:

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
    define("SHARE_SEND_EXPIRATION", 14);
}

如果有人能告訴我如何避免重新定義此單元測試,以避免修改Command類的內部,那將是最有幫助的。 這個單元測試似乎只是一個問題。

對於每個測試方法,為phpunit添加兩個注釋,如下所示:

/* @runInSeparateProcess
 * @preserveGlobalState disabled
 */
public function testPageShow()
{
  • runInSeparateProcess:在單獨的進程中運行每個測試
  • preserveGlobalState disabled:禁用先前測試的全局狀態

我認為對於臨時解決方案,雖然你仍然不確定在哪里放置那些全局常量,但是請使用defined

defined('SAMPLE_GLOBAL_CONSTANT')
  or define('SAMPLE_GLOBAL_CONSTANT', 'sample value of the constant')

順便說一句,如果你還運行全局函數的重定義異常,你也可以使用function_exists方法。

但是請確保使用Class來保存全局函數,以便您可以輕松地測試應用程序。 就像把它放在像Helper這樣的東西上,或者像StringHelper那樣更具體。

使用SenseException的解決方案來刪除所有的define語句(事實證明在整個應用程序中只使用了兩個),而使用類常量確實解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM