Skip to content

Instantly share code, notes, and snippets.

@scottgrayson
Last active October 21, 2016 14:25
Show Gist options
  • Save scottgrayson/61ca73ca096632d8ea44f538b7ad3f16 to your computer and use it in GitHub Desktop.
Save scottgrayson/61ca73ca096632d8ea44f538b7ad3f16 to your computer and use it in GitHub Desktop.
API testing with json() and seeJson()
<?php
use App\User;
use App\Profile;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateUserTest extends TestCase
{
use DatabaseMigrations, WithoutMiddleware;
/**
* A basic functional test example.
*
* @return void
*/
public function testUpdatingUser()
{
$user = factory(User::class)->create(['username' => 'my-username']);
$profile = factory(Profile::class)->make(['bio' => 'my-bio']);
$user->profile()->save($profile);
$this->be($user);
$this->json('PUT', '/api/users/'.$user->uuid, [
'bio' => 'new-bio',
'username' => 'new-username'
])->seeJson([
'success' => true
]);
$this->visit('/api/users/'.$user->uuid)
->see('new-bio')
->see('new-username');
}
}
@scottgrayson
Copy link
Author

scottgrayson commented Oct 21, 2016

rewrote it and the ->assertResponseOk() method is giving me better errors if i purposely put one in the controller

<?php

use App\User;
use App\Profile;

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserRestTest extends TestCase
{
    use DatabaseMigrations, WithoutMiddleware;

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testUpdatingUser()
    {
        $user = factory(User::class)->create(['username' => 'my-username']);

        $profile = factory(Profile::class)->make(['bio' => 'my-bio']);

        $user->profile()->save($profile);

        $this->be($user);

        $this->put('/api/users/'.$user->uuid, [
            'bio' => 'new-bio',
            'username' => 'new-username'
        ])->assertResponseOk();

        $this->get('/api/users/'.$user->uuid)
            ->seeJson([
                'bio' => 'new-bio',
                'username' => 'new-username'
            ]);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment