【问题标题】:Mock get parameters symfony2 and phpunit模拟获取参数 symfony2 和 phpunit
【发布时间】:2017-03-14 11:47:45
【问题描述】:

我想测试一个控制器:

$deviceGet = strtolower($request->query->get('device'));

我的问题是:如何模拟它以在我的控制器中进行测试?

class SetDeviceListenerTest extends \PHPUnit_Framework_TestCase {


     /**
     * @test
     * @dataProvider deviceGetCases
     */
public function shouldSetRequestDeviceWithDeviceGetOnMasterRequest(
    ?string $device
): void {
    $request = $this->getRequestMock();

    $request->expects($this->once())
        ->method('get')
        ->with('device')
        ->willReturn($device);


    /* @var Request $request */
    $request->server = new ServerBag();
    $request->headers = new HeaderBag();

    $event = $this->getEventMock();
    $event->expects($this->once())
        ->method('isMasterRequest')
        ->willReturn(true);

返回错误是因为

 $request->expects($this->once())
        ->method('get')
        ->with('device')
        ->willReturn($device);

失败

谢谢!!

【问题讨论】:

    标签: symfony unit-testing phpunit


    【解决方案1】:

    我自己找到了答案(我只是需要一个朋友):

    $request->query= new ParameterBag(['device'=>$device]);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2012-12-19
      • 2014-03-18
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多