我在Laravel 4中测试工作时遇到了一些麻烦.我正在使用.env文件管理我的数据库设置,就像它在
Laravel’s Configuration manual – Protecting Sensitive Configuration中所描述的那样
app / config / database.php文件如下所示:
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['dbhost'],
'database' => $_ENV['database'],
'username' => $_ENV['dbusername'],
'password' => $_ENV['dbpassword'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
正在测试的控制器方法:
public function getTaxonomies()
{
if (Input::has('page')) {
$limit = (Input::get('limit')) ? Input::get('limit') : 15;
$taxonomy = Taxonomy::with('photos')->paginate($limit)->toArray();
return Response::json(array(
'Taxonomies' => $taxonomy
));
}
return Response::json(array(
'Taxonomies' => Taxonomy::all()->load('photos')->toArray()
));
}
考试:
<?php
# app/tests/controllers/TaxonomyControllerTest.php
class TaxonomyControllerTest extends TestCase
{
public function testGetTaxonomies()
{
$this->action('GET', 'TaxonomyController@getTaxonomies');
$this->assertResponseOk();
}
}
我得到的错误是ErrorException:未定义的索引:dbhost.我意识到这是因为$_ENV var没有在CLI中填充.所以我的问题是,我应该如何处理db信用卡进行测试?
更新:
所以我在我的app / config / testing文件夹中添加了一个空的database.php文件,现在我不再收到该错误了.我假设是因为数据库不再被调用了?我应该使用嘲弄来测试数据吗?
最佳答案
Note: You may create a file for each environment supported by your
application. For example, the development environment will load the
.env.development.php file if it exists.
只需创建.env.testing.php文件并在其中编写测试数据库凭据.
return array(
/*
|--------------------------------------------------------------------------
| Database Credentials
|--------------------------------------------------------------------------
*/
'dbhost' => '127.0.0.1',
'database' => 'database',
'dbusername' => 'username',
'dbpassword' => 'password',
);