Laravel测试:协调http请求和控制器

时间:2018-08-17 07:42:41

标签: laravel unit-testing phpunit laravel-testing phpunit-testing

在下面的测试中,我们(非常谦卑)需要确保$utmSource类字段收到正确的值。

<?php

class ExampleTest extends TestCase {

  public function testHttpRequestAndControllerCoordination() {

    $response = $this->call('GET', '/')->json([utm_source => 'Google']);
    $topPageController = new TopPageController();
    $topPageController->renderTopPage();

    $this->assertAttributeSame('Google', 'utm_source', $topPageController);
  }
}

class TopPageController extends Controller {

    private $utmSource;

    public function renderTopPage(){
        $this->utmSource = request()->utm_source;
    }
}

当然,该测试不会通过,因为$response$topPageController是独立的,所以$utmSource将是null。我们如何关联$response$topPageController

0 个答案:

没有答案