Undefined $response property when testing Laravel API
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.
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
This is the test function:
public function testBasicExample()
{
$token = Token::find(1);
$this->get('v1/companies?token='.$token->token)->seeJson();
}
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
Thanks Stephan, that solved the problem.
@arrahman you're welcome :)
+1
+1 Awesome. Thank you!
+1, nice quick solution. Guessing this package isn't going to be updated since it got integrated with L5. All good though.