![](/img/trans.png)
[英]When running unit tests with laravel, how do you test your App::error() implementations?
[英]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()方法,這可能解釋為什么Command
類DeleteExpiredSends
被重新定義; 該類在其構造函數中顯示一個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()
{
我認為對於臨時解決方案,雖然你仍然不確定在哪里放置那些全局常量,但是請使用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.