gpt4 book ai didi

unit-testing - 调用模型的 Laravel 4 测试 Controller

转载 作者:行者123 更新时间:2023-12-01 03:49:21 25 4
gpt4 key购买 nike

我在 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' => '',
),

正在测试的 Controller 方法:
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: Undefined index: dbhost .我意识到这是因为 $_ENV CLI 中未填充 var。所以我的问题是,我应该如何处理 db creds 进行测试?

更新:

所以我添加了一个空的 database.php文件到我的 app/config/testing文件夹,现在我不再收到该错误。我假设这是因为不再调用数据库?我应该只是使用 mock 来测试数据吗?

最佳答案

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',

);

关于unit-testing - 调用模型的 Laravel 4 测试 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24251636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com