duansha8115 2014-07-21 06:03
浏览 35
已采纳

在laravel-4中使用phpunit测试store()时的undefined变量

SOLVED

I have route that does a POST route towards store() in the controller.

I'm trying to test if the action is working properly.

Controller:

public function store() {
    $d= Input::json()->all();

    //May need to check here if authorized

    $foo= new Foo;
    $d = array();
    $d['name'] = $d['name'];
    $d['address'] = $d['address'];
    $d['nickname'] = $d['nickname'];



    if($foo->validate($d))
    {
        $new_foo= $foo->create($d);

        return Response::json(Foo::where('id','=',$new_foo->id)->first(),200);
    }
    else
    {
        return Response::json($foo->errors(),400);
    }
}

Now I'm trying to test this using a new class called FooTest.php

Here is the function i'm currently trying to do to make the check work:

 public function testFooCreation()
{

    $jsonString = '{"address": "82828282", "email": "test@gmail.com", "name":"Tester"}';

    $json = json_decode($jsonString);

    $this->client->request('POST', 'foo');

    $this->assertEquals($json, $this->client->getResponse());

}

when I run phpunit in my cmd, I get an error stating that "name" is undefined. I know i'm not actually passing anything to the request so I'm positive that nothing is actually being checked, but my question is how do I actually pass my json strings to check?

Everytime I put the $json inside the client request, it asks for an array, but when I convert my json string to an array, json_decode wants a string.

UPDATE

I was messing around with the passing of input data and I came across this:

 $input = [
        'name' => 'TESTNAME',
        'address' => '299 TESTville',
        'nickname' => 't'
        ];


     Input::replace($input);

     Auth::shouldReceive('attempt')
        ->with(array('name' => Input::get('name'),
                     'address' => Input::get('address'),
                     'nickname' => Input::get('nickname')))
        ->once()
        ->andReturn(true);


     $response = $this->call('POST', 'foo', $input);
     $content = $response->getContent();
     $data = json_decode($response->getContent());

But whenever I run the test, i still get "name:undefined" It's still not passing the input i've created.

  • 写回答

3条回答 默认 最新

  • dongyuelian9602 2014-07-22 04:53
    关注

    I was able to pass the input into a POST route from the test.

     public function testFooCreation(){
    
          $json = '{"name":"Bar", "address":"FooLand", "nickname":"foobar"}';
    
          $post = $this->action('POST', 'FooController@store', null, array(), array(), array(), $json);
    
          if($this->assertTrue($this->client->getResponse()->isOk()) == true && $this->assertResponseStatus(201)){
    
          echo "Test passed";
          }
    
     }
    

    Turns out that in order for me to actually pass input into the controller through test POST, I have to pass it through the 7th parameter.

    I hope this helps others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀