【发布时间】:2017-03-01 12:49:28
【问题描述】:
嗨,我有这个单元测试
public function test_index_coupon(){
$coupons = factory(App\Models\Coupon::class, 5)->create();
$this->visit('/admin/coupons')
->assertResponseOk()
->assertViewHasAll('coupons');
}
这是我的控制器来列出优惠券索引
public function index()
{
$coupons = Coupon::all();
return view('backend.admin.coupons.index', compact('coupons'));
}
而且我可以用
成功列出优惠券@foreach($coupons as $coupon)
.....
@endforeach
在我看来,我是通过浏览器查看的。
但是当我运行phpunit 时,我得到了这个错误
1) CouponsTest::test_index_coupon
ErrorException: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::assertViewHasAll() must be of the type array, string given, called in /var/www/html/rentcar/tests/admin/CouponsTest.php on line 24 and defined
然后我尝试将我的测试从 ->assertViewHasAll('coupons'); 修改为 ->assertViewHas('coupons'); 并得到不同的错误
1) CouponsTest::test_index_coupon
Failed asserting that an array has the key 'coupons'.
我的测试代码有什么问题?我只想检查访问admin/coupons 优惠券列表是否正确加载。所以我可以确定$coupons 是否存在于视图中。
更新
它适用于 assertViewHas('coupons') 不知道为什么,可能是我的整个测试代码有问题,或者可能是因为 use WithoutMiddleware
感谢您的回答和评论。
【问题讨论】: