我想用Laravel 4进行单元测试。但是我有一个问题。
这是我的控制器
class HalisahalarController extends BaseController {
public function getIndex(){
// halı sahaların bilgilerini toplamak için bir dizi değişken oluşturulur
$halisahalar = [];
// halı sahaların foreach içinde gerekli bilgileri alınır
foreach (HalisahaAccount::with(
'halisahaInformation',
'halisahaAdress',
'services',
'halisahaCoverPhoto',
'halisahaUrl'
)->get() as $halisaha) {
$hs['id'] = $halisaha->id; #halı saha id
$hs['name'] = $halisaha->halisahaInformation->halisaha_name; #halı saha ad
$hs['adress']['province'] = isset($halisaha->halisahaAdress) ? $halisaha->halisahaAdress->province->province : -1; # halı saha il
$hs['adress']['county'] = isset($halisaha->halisahaAdress) ? $halisaha->halisahaAdress->county->county : -1; #halı saha ilçe
$hs['services'] = $halisaha->services->toArray(); #halı saha servisleri
$hs['coverPhoto'] = $halisaha->halisahaCoverPhoto->toArray(); #halı saha kapak foto
$hs['halisahaUrl'] = isset($halisaha->halisahaUrl) ? $halisaha->halisahaUrl->url : -1; #halı saha url
// alınan veriler dizi değişken içine itilir
array_push($halisahalar,$hs);
}
return View::make('index',array('halisahalar' => $halisahalar));
}
}
这是我的测试代码
class HalisahalarControllerTest extends TestCase {
/**
* /halisahalar test
*
* @dataProvider halisahaDatas
*/
public function testGetIndex($halisaha)
{
$crawler = $this->client->request('GET', '/halisahalar');
// yanıt başarılı bir şekilde geldi mi
$this->assertResponseOk();
// 200 kodu geldi mi
$this->assertResponseStatus(200);
}
/**
* halisaha datas
*/
public function halisahaDatas () {
return [
[
'id' => 1,
'name' => 'Lider Halı Saha',
'adress' => [
'province' => 'İstanbul',
'county' => 'Sultanbeyli'
],
'services' => [
[
'service' => 'Duş',
'icon' => 'dus.png'
],
[
'service' => 'Çeşitli Oyunlar',
'icon' => 'oyun.png'
]
],
'coverPhoto' => [
[
'photo' => 'lider4.jpg'
]
],
'halisahaUrl' => 'lider'
],[
'id' => 2,
'name' => 'Çalışkan Halı Saha',
'adress' => [
'province' => 'İstanbul',
'county' => 'Sancaktepe'
],
'services' => [
[
'service' => 'Duş',
'icon' => 'dus.png'
],
[
'service' => 'İnternet',
'icon' => 'wifi.png'
]
],
'coverPhoto' => [
],
'halisahaUrl' => 'caliskan-halisaha'
]
];
}
}
我收到以下错误:
1) HalisahalarControllerTest::testGetIndex with data set #0 (1, 'Lider Halı Saha', array('İstanbul', 'Sultanbeyli'), array(array('Duş', 'dus.png'), arra
y('Çeşitli Oyunlar', 'oyun.png')), array(array('lider4.jpg')), 'lider')
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'halisaha_HSHadm'@'localhost' (using password: YES)
2) HalisahalarControllerTest::testGetIndex with data set #1 (2, 'Çalışkan Halı Saha', array('İstanbul', 'Sancaktepe'), array(array('Duş', 'dus.png'),
array('İnternet', 'wifi.png')), array(), 'caliskan-halisaha')
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'halisaha_HSHadm'@'localhost' (using password: YES)
我不想在测试中使用数据库。我想在 halisahaDatas 方法中使用数据。我认为php单元试图连接数据库并收到错误。
答案 0 :(得分:1)
您应该在控制器中使用Laravel依赖注入,如下所示:
class HalisahalarController extends BaseController {
protected $halisahaAccount;
public function __construct(HalisahaAccount $halisahaAccount){
$this->halisahaAccount = $halisahaAccount;
}
public function getIndex(){
$yourAccounts = $this->halisahaAccount->with(
'halisahaInformation',
'halisahaAdress',
'services',
'halisahaCoverPhoto',
'halisahaUrl'
)->get();
}
}
在你的测试中,你会做这样的事情:
class HalisahalarControllerTest extends TestCase {
/**
* /halisahalar test
*
* @dataProvider halisahaDatas
*/
public function testGetIndex($halisaha)
{
$mockHalisahaAccount = new Mockery::mock('HalisahaAccount');
$mockHalisahaAccount->shouldReceive('with')->once()
->with('halisahaInformation',
'halisahaAdress',
'services',
'halisahaCoverPhoto',
'halisahaUrl')->andReturnSelf();
$mockHalisahaAccount->shouldReceive('get')->once()->andReturn($this->halisahaDatas());
$this->app->instance('HalisahaAccount', $mockHalisahaAccount);
$crawler = $this->client->request('GET', '/halisahalar');
// yanıt başarılı bir şekilde geldi mi
$this->assertResponseOk();
// 200 kodu geldi mi
$this->assertResponseStatus(200);
}
}