【问题标题】:Laravel 5 unit testing. Unable to set JSON of Request objectLaravel 5 单元测试。无法设置请求对象的 JSON
【发布时间】:2015-06-12 06:54:45
【问题描述】:

到底如何让 Laravel 5.0 接受 JSON 编码的字符串到它的请求对象中?因为我的 REST api 返回了 500 个错误,并且经过仔细检查,请求对象有一个空的 json 属性...?

我的数组:

    private $test_1_create_user = array(
        "name" => "Mr T Est",
        "email" => "mrtest@somedomain.com",
        "password" => "testing1234"
    );

我的测试方法:

    /**
    * Attempts to Create a single user with no permissions
    */
    public function testCreateUser(){
        /** Obtain instance of Request object */
        $req = $this->app->request->instance();
        /** Set the JSON packet */
        $req->json(json_encode($this->test_1_create_user));
        /** Run the test */
        $response = $this->call('POST', '/api/v1/user');
        /** Read the response */    
        $this->assertResponseOk();
    }

还有 $req 的 var_dump(稍微精简了一点):

C:\wamp\www\nps>php phpunit.phar
PHPUnit 4.6.2 by Sebastian Bergmann and contributors.

Configuration read from C:\wamp\www\nps\phpunit.xml

class Illuminate\Http\Request#34 (25) {
  protected $json =>
  class Symfony\Component\HttpFoundation\ParameterBag#261 (1) {
    protected $parameters =>
    array(0) {
    }
  }
  protected $sessionStore =>
  NULL
  protected $userResolver =>
  NULL
  protected $routeResolver =>
  NULL
  public $attributes =>
  class Symfony\Component\HttpFoundation\ParameterBag#41 (1) {
    protected $parameters =>
    array(0) {
    }
  }
  public $request =>
  class Symfony\Component\HttpFoundation\ParameterBag#43 (1) {
    protected $parameters =>
    array(0) {
   }
 }

我花了很长时间才弄清楚如何从单元测试中访问请求对象。有人知道为什么$req->json 总是空的吗? :( 干杯!

【问题讨论】:

    标签: php json unit-testing phpunit laravel-5


    【解决方案1】:

    显然我把事情复杂化了。对于其他可能在将 json 发布到 Laravel 控制器(在单元测试内部)时遇到问题的人,我只是通过以下方式解决了它:

    $response = $this->call('POST', '/api/v1/user', $this->test_1_create_user);
    

    关键元素是最后一个参数,它是一个 php 数组。这会在 POST 之前“神奇地”转换为 json。非常缺乏这方面的文档......

    【讨论】:

      【解决方案2】:

      您尝试设置 json 值的方式不正确,因为 Request 中的 json 方法旨在从请求中获取 JSON 值,而不是设置它们。您需要为您的测试重新初始化 Request 对象。像这样的东西应该可以为您解决问题:

      /**
      * Attempts to Create a single user with no permissions
      */
      public function testCreateUser(){
          /** Obtain instance of Request object */
          $req = $this->app->request->instance();
          /** Initialize the Request object */
          $req->initialize(
              array(), // GET values
              array(), // POST values
              array(), // request attributes
              array(), // COOKIE values
              array(), /// FILES values
              array('CONTENT_TYPE' => 'application/json'), // SERVER values
              json_encode($this->test_1_create_user) // raw body content
          );
          /** Run the test */
          $response = $this->call('POST', '/api/v1/user');
          /** Read the response */    
          $this->assertResponseOk();
      }
      

      请记住,您可能希望根据需要填充其他请求值,我只包含了 Content-Type 和 json 内容

      【讨论】:

      • 感谢您的宝贵时间,但不幸的是,即使手动设置每个字段以匹配默认请求对象,这仍然会导致我的 API 说“名称”不能为空,因此 JSON 仍然是'没有通过:(
      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多