mercredi 19 octobre 2016

Laravel 5.2 PHPUnit assertViewHasAll() Failed

Hi I have this unit test

public function test_index_coupon(){

    $coupons = factory(App\Models\Coupon::class, 5)->create();      

    $this->visit('/admin/coupons')
    ->assertResponseOk()
    ->assertViewHasAll('coupons');
}

Here is my controller to list index of coupons

   public function index()
    {
        $coupons = Coupon::all();   
        return view('backend.admin.coupons.index', compact('coupons'));
    }

And I can successfully list coupons with

@foreach($coupons as $coupons)
.....
@endforeach

In my view, I check it via browser.

But when I run phpunit I get this error

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

Then I try modify my test from ->assertViewHasAll('coupons'); to ->assertViewHas('coupons'); and I get different error

1) CouponsTest::test_index_coupon
Failed asserting that an array has the key 'coupons'.

Whats wrong with my test code? I just want to check if visit admin/coupons coupons list loaded properly. so I can make sure if $coupons is exists in view.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire