laracasts / Integrated

Simple, intuitive integration testing with PHPUnit.
https://laracasts.com/series/intuitive-integration-testing
MIT License
474 stars 87 forks source link

Undefined $response property when testing Laravel API #70

Open hskrasek opened 9 years ago

hskrasek commented 9 years ago

I am trying to test a JSON API written in Laravel 4.2. I have the following test case below:

public function testWaterfallAPIDoesNotReturnWaterfallForUnkownBundleId()
 {
    $this->app->bind('\Example\Common\Model\v2\AdWaterfallRepositoryInterface', function () {
      $databaseConnectionMock = Mockery::mock();
      $databaseManagerMock    = Mockery::mock('Illuminate\Database\DatabaseManager');
      $databaseManagerMock->shouldReceive('connection')->once()->andReturn($databaseConnectionMock);

       $cacheMock = Mockery::mock('Illuminate\Cache\Repository');
       $cacheMock->shouldReceive('remember')->once()->andReturn(null);

       return new \Example\Common\Model\v2\DbAdWaterfallRepository(
            $databaseManagerMock,
            $cacheMock
        );
    });

    $this->get('v2/waterfalls/com.perk.noteven.json')->seeJson();
 }

And when I run the tests I get the following in the terminal:

1) GenericAPITest::testWaterfallAPIDoesNotReturnWaterfallForUnkownBundleId
ErrorException: Undefined property: GenericAPITest::$response

/home/vagrant/perk-tv-api/vendor/laracasts/integrated/src/Extensions/Traits/LaravelTestCase.php:113
/home/vagrant/perk-tv-api/vendor/laracasts/integrated/src/Extensions/Traits/ApiRequests.php:103
/home/vagrant/perk-tv-api/app/tests/GenericAPITest.php:31

Not sure if this is a bug, or something I am doing incorrectly.

arrahman commented 9 years ago

I m getting the same error:

ErrorException: Undefined property: CompanyTest::$response

/var/www/html/intrepid-api/vendor/laracasts/integrated/src/Extensions/Traits/LaravelTestCase.php:113 /var/www/html/intrepid-api/vendor/laracasts/integrated/src/Extensions/Traits/ApiRequests.php:170 /var/www/html/intrepid-api/app/tests/controllers/CompanyTest.php:9

arrahman commented 9 years ago

This is the test function:

public function testBasicExample()
{
        $token = Token::find(1);

        $this->get('v1/companies?token='.$token->token)->seeJson();
}
stephanecoinon commented 9 years ago

This error happens because the class Illuminate\Foundation\Testing\ApplicationTrait does not have the $response property in Laravel 4. But you can add it in like this:

Providing you're extending Laracasts\Integrated\Extensions\Laravel in TestCase class, try and add this method in app/tests/TestCase.php:

public function __get($name)
{
    if ($name === 'response') return $this->client->getResponse();
}

If you extend Laracasts\Integrated\Extensions\Laravel directly in your test class, just add the method above in it

arrahman commented 9 years ago

Thanks Stephan, that solved the problem.

stephanecoinon commented 9 years ago

@arrahman you're welcome :)

lukrizal commented 9 years ago

+1

vjrngn commented 9 years ago

+1 Awesome. Thank you!

llaski commented 8 years ago

+1, nice quick solution. Guessing this package isn't going to be updated since it got integrated with L5. All good though.