测试扩展Controller类的控制器

时间:2017-05-17 16:29:11

标签: php symfony unit-testing phpunit

我尝试实例扩展Controller

的类

这是我的控制器:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class SalaryController extends Controller
{
    public function calculateTotalSalary($id)
    {
        return $this->render('resources/view.html.twig');
    }
}

这就是我要调用控制器的地方

<?php

namespace AppBundle\Tests\Units\Controller\Controller;

use AppBundle\Controller\SalaryController;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class SalaryControllerTest extends KernelTestCase
{
    public function testExample()
    {
        $controller = new SalaryController;
        $controller->calculateTotalSalary(1);
    }
}

执行时代码给我这个:

Starting test 'AppBundle\Tests\Units\Controller\Controller\SalaryControllerTest::testExample'.
PHP Fatal error:  Call to a member function get() on null \vendor
symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php on line 176

Controller类中的第176行:

return $this->container->get('templating')->renderResponse($view, $parameters, $response);

$ container变量为null,为什么?

0 个答案:

没有答案