我的ProduitsControllerTest中的testRapportParams函数有问题。当我使用变量输入url时,它会通过控制器传递else部分,这意味着该变量未设置。问题是,在核心程序中,ProduitsController运行良好。我不明白它在我的测试中不起作用。
这是我的测试控制器的代码:
fs/2
以下是我用于测试的功能:
//in my ProduitsControllerTest
public function testRapportParams()
{
//Test if the year in the url can be found
$dateCompare = array(Date('Y'), Date('Y')-1, Date('Y')-2, Date('Y')-3,Date('Y')-4,Date('Y')-5,Date('Y')-6,Date('Y')-7);
$produits = TableRegistry::get('Produits');
//The url called by this is : '/produits/rapport?datecompare=2015'
$this->get('/produits/rapport?datecompare='.$datecompare[1]);
$this->assertResponseOk();
$produitsnouveau = $this->viewVariable('produitsnouveau');
$produitssupp = $this->viewVariable('produitssupp');
//$produits->find('all')->where(['YEAR(__creation_ts) = ' => $datecompare[1]])
// ->where('supp_le_ts is NULL')
// ->count() == 0
//$produits->find('all')->where(['YEAR(supp_le_ts) = ' => $datecompare[1]])
// ->count() == 2
//count($produitsnouveau) == 0 (supposed to be 0)
//count($produitssupp) == 1 (supposed to be 2)
$this->assertEquals($produits->find('all')->where(['YEAR(__creation_ts) = ' => $datecompare[1]])->where('supp_le_ts is NULL')->count(),count($produitsnouveau));
$this->assertEquals($produits->find('all')->where(['YEAR(supp_le_ts) = ' => $datecompare[1]])->count(),count($produitssupp));
}
我想了解为什么在我的测试中使用它时总是会传入else部分,但是当我在主应用程序中使用我的浏览器时它运行良好。
答案 0 :(得分:0)
这样做是因为您没有使用正确的CakePHP技术来检索请求数据,即\Cake\Network\Request
对象,特别是Request::query()
,如
$datecompare = $this->request->query('datecompare');
if ($datecompare !== null) {
// ...
集成测试用例只是模拟请求,它不会调度真实的,因此,$_GET
将不会被填充,因为它会污染全局状态,而是数据将直接设置为请求对象
见