我試圖運行PHPunit測試訪問('/')在LAravel 5但它失敗,下面這條消息: PHPUnit在Laravel錯誤:異常''與消息'SQLSTATE [HY000] [2005]未知MySQL服務器主機'mysql'(2)'
class MyTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
public function providerAllUrisWithResponseCode()
{
return [
['/', 200],
['/thank', 200],
['/non-existing', 404],
];
}
public function testDisplayThankYou($value='')
{
$this->visit('/thankyou')
->see('Thank you!');
}
public function testPageControllerGet()
{
$this->call('GET', '/');
}
public function testDisplayWelcome($value='')
{
$this->visit('/')
->see('Welcome');
}
}
(我的應用程序在泊塢窗容器http://www.spiralout.eu/2015/12/dockervel-laravel-development.html運行),我收到此錯誤。
$vendor/bin/phpunit
PHPUnit 4.8.26 by Sebastian Bergmann and contributors.
...F
Time: 24.01 seconds, Memory: 16.00MB
There was 1 failure:
1) MyTest::testDisplayWelcome
A request to [http://localhost] failed. Received status code [500].
..vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
..vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
..vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:61
.../www/tests/ExampleTest.php:16
Caused by
exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)'
in ../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
我的behat功能通過了! 。 我.ENV *文件看起來像:
.env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
.env.behat
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
另外:如果我刪除或更改DB_HOST = 127.0.0.1在.env.behat和我跑貝哈特它返回一個錯誤!
在Connector.php線50說:
public function createConnection($dsn, array $config, array $options)
{
**here it breaks**--> $username = Arr::get($config, 'username');
$password = Arr::get($config, 'password');
try {
$pdo = new PDO($dsn, $username, $password, $options);
} catch (Exception $e) {
$pdo = $this->tryAgainIfCausedByLostConnection(
$e, $dsn, $username, $password, $options
);
}
return $pdo;
}
例如,當我運行DB ::表( '東西') - >獲得();它工作正常。
有人看到這個問題?
似乎是一個環境配置問題,DB_HOST必須在測試環境中設置爲'mysql'。 – Devon
如果您的.env中的DB_HOST設置爲'localhost'而不是'mysql',該怎麼辦? – ishadif
@Devon是我的測試環境,與Laravel應用程序相同。環境? – moonlight