Integrated icon indicating copy to clipboard operation
Integrated copied to clipboard

Undefined $response property when testing Laravel API

Open hskrasek opened this issue 10 years ago • 8 comments

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.

hskrasek avatar May 07 '15 21:05 hskrasek

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 avatar May 29 '15 20:05 arrahman

This is the test function:

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

        $this->get('v1/companies?token='.$token->token)->seeJson();
}

arrahman avatar May 29 '15 20:05 arrahman

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

stephanecoinon avatar May 31 '15 17:05 stephanecoinon

Thanks Stephan, that solved the problem.

arrahman avatar Jun 02 '15 15:06 arrahman

@arrahman you're welcome :)

stephanecoinon avatar Jun 02 '15 20:06 stephanecoinon

+1

lukrizal avatar Jun 16 '15 06:06 lukrizal

+1 Awesome. Thank you!

vjrngn avatar Jun 25 '15 05:06 vjrngn

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

llaski avatar Jan 05 '16 00:01 llaski