Laravel控制器布局测试

Cri*_*ane 2 php unit-testing controller laravel laravel-4

我正在尝试将单元测试包含在我正在构建的新Laravel应用程序中.现在我想测试我的OrderController.该控制器的索引方法如下所示:

public function index()
{
    // We need the extra 'orders()' for the query scope
    $orders = $this->order->orders()->paginate($this->perPage);

    $this->layout->content = View::make('orders.index', compact('orders'));
}
Run Code Online (Sandbox Code Playgroud)

现在我的测试看起来像这样:

public function testIndex()
{
    $this->call('GET', 'orders');

    $this->assertResponseOk();

    $this->assertViewHas('orders');
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行phpunit,测试确实运行,但我得到: 1) OrderControllerTest::testIndex Failed asserting that an array has the key 'orders'.

我已经跟踪了使用Controller Layouts的问题$this->layout.如果我只是做return View::make()测试确实通过,如果我返回$this->layout...它也确实通过,但这会破坏实际的应用程序.

因此,我发现只有选项才能使用return View::make()并拥有@extends('master')该视图.但是我觉得如果你想测试它就不能在你的应用程序中使用Controller Layouts,这似乎很奇怪.

难道我做错了什么?

ney*_*eyl 9

编辑: 我有同样的问题,这里有点凌乱的解决方案,有效!

    View::composer('ordersviewname', function($view) {
      $this->assertArrayHasKey('orders', $view->getData());
    });

    $this->call('GET', 'orders');
Run Code Online (Sandbox Code Playgroud)

请注意,您必须先放置此代码 $this->call()

编辑: 这是更优雅的解决方案!向TestCase添加功能

    protected $nestedViewData = array();

    public function registerNestedView($view)
    {
      View::composer($view, function($view){
        $this->nestedViewsData[$view->getName()] = $view->getData();
      }); 
    }

    /**
     * Assert that the given view has a given piece of bound data.
     *
     * @param  string|array  $key
     * @param  mixed  $value
     * @return void
     */
    public function assertNestedViewHas($view, $key, $value = null)
    {
        if (is_array($key)) return $this->assertNestedViewHasAll($view, $key);

        if ( ! isset($this->nestedViewsData[$view]))
        {
            return $this->assertTrue(false, 'The view was not called.');
        }

        $data = $this->nestedViewsData[$view];

        if (is_null($value))
        {
            $this->assertArrayHasKey($key, $data);
        }
        else
        {
            if(isset($data[$key]))
              $this->assertEquals($value, $data[$key]);
            else 
              return $this->assertTrue(false, 'The View has no bound data with this key.');            
        }
    }

    /**
     * Assert that the view has a given list of bound data.
     *
     * @param  array  $bindings
     * @return void
     */
    public function assertNestedViewHasAll($view, array $bindings)
    {
        foreach ($bindings as $key => $value)
        {
            if (is_int($key))
            {
                $this->assertNestedViewHas($view, $value);
            }
            else
            {
                $this->assertNestedViewHas($view, $key, $value);
            }
        }
    }

    public function assertNestedView($view)
    {
      $this->assertArrayHasKey($view, $this->nestedViewsData);
    }
Run Code Online (Sandbox Code Playgroud)

现在你要重写你的测试

$view='orders';
$this->registerNestedView($view);

$this->call('GET', 'orders');

$this->assertNestedViewHas($view, 'orders');
Run Code Online (Sandbox Code Playgroud)

assertNestedViewHas()拥有所有相同的功能assertViewHas()!

我添加了另一个函数assertNestedView(),只检查是否调用了给定的视图.