2

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

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

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

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

    $this->assertResponseOk();

    $this->assertViewHas('orders');
}

现在,如果我运行 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')但是如果你想测试它,你不能在你的应用程序中使用控制器布局,这对我来说似乎很奇怪。

难道我做错了什么?

4

1 回答 1

9

编辑: 我遇到了同样的问题,这里有一个有点混乱的解决方案!

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

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

请注意,您必须先输入此代码$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);
    }

现在你会重写你的测试

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

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

$this->assertNestedViewHas($view, 'orders');

assertNestedViewHas()具有所有相同的功能assertViewHas()

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

于 2013-06-04T13:51:33.893 回答