【问题标题】:undefined variable when testing the store() using phpunit in laravel-4在 laravel-4 中使用 phpunit 测试 store() 时未定义的变量
【发布时间】:2014-09-11 13:43:53
【问题描述】:

已解决

我的路由在控制器中向 store() 执行 POST 路由。

我正在尝试测试该操作是否正常工作。

控制器:

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);
    }
}

现在我正在尝试使用一个名为 FooTest.php 的新类来测试它

这是我目前正在尝试执行的使检查工作的功能:

 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());

}

当我在 cmd 中运行 phpunit 时,我收到一条错误消息,指出“名称”未定义。我知道我实际上并没有向请求传递任何东西,所以我很肯定实际上没有任何检查,但我的问题是我如何实际传递我的 json 字符串来检查?

每次我将 $json 放入客户端请求中时,它都会请求一个数组,但是当我将我的 json 字符串转换为数组时,json_decode 需要一个字符串。

更新

我在处理输入数据的传递时遇到了这个问题:

 $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());

但是每当我运行测试时,我仍然得到“name:undefined”它仍然没有通过我创建的输入。

【问题讨论】:

    标签: php json laravel-4 phpunit


    【解决方案1】:
    $d= Input::json()->all();
    

    以上语句在$d中获取Input。

    $d = array();
    

    现在最后一条语句再次将 $d 初始化为一个空的新数组

    所以没有: $['name'] 。因此,未定义。

    我认为,这就是上面代码的问题。

    希望对你有帮助:)

    【讨论】:

    • 正确。我知道哈哈。我的问题实际上不是为什么我的“名字”未定义。我的问题是如何使用 phpunit 实际测试它。我想使用我的 store() 测试函数传入数据并检查它是否真的有效。
    • 检查我的更新。也许我的问题会更清楚。
    【解决方案2】:

    我能够将输入传递到来自测试的 POST 路由中。

     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";
          }
    
     }
    

    事实证明,为了让我通过测试 POST 将输入实际传递给控制器​​,我必须通过第 7 个参数传递它。

    我希望这对其他人有所帮助。

    【讨论】:

      【解决方案3】:

      当然你得到一个错误,看看你的代码

      $aInputs = 输入::json()->all();
      
          //可能需要在这里检查是否授权
      
          $foo=新的Foo;
          $d = 数组();
          $d['name'] = $d['name'];
          $d['地址'] = $d['地址'];
          $d['昵称'] = $d['昵称'];

      您将数组分配给它自己,它是空的

      【讨论】:

      • 你怎么又开了一个问题,问题和stackoverflow.com/questions/24857763/…中的一样
      • 是的,我在回答中澄清说,我当然遇到了错误。我的问题是我如何实际做一个测试,输入示例输入来检查 store() 是否在做它应该做的事情。
      • 我打开了另一个问题,因为这个问题在我正在寻找的过程方面更加具体。另一个问题似乎有点草率。
      猜你喜欢
      • 2016-08-10
      • 2014-09-28
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2016-04-24
      • 2014-04-28
      相关资源
      最近更新 更多