1
votes

I'm using codeception with laravel4 to test my API. I have a set of functions that need to be used many times in different tests. I'm trying to use StepObjects to do this. based on an example in this link here is my code: The code in the _steps folder

namespace ApiTester;
 
 class generalSteps extends \ApiTester {
 
     public function json_format( $path = "/users/me", $desc = "TEST") {
         $I = $this;
         $I->wantTo($desc);
         $I->amBearerAuthenticated(\LoginCest::$token);
         $I->sendPOST($path);
        $I->seeResponseJsonMatchesJsonPath('$.content.data');
    } }

my _bootstrap.php file:

  \Codeception\Util\Autoload::registerSuffix('Steps',
     __DIR__.DIRECTORY_SEPARATOR.'_steps');

And this is how I try to access it in a function under API folder:

public function correct_json_format(ApiTester $I) {
       $I= new \ApiTester\generalSteps($scenario);
        $I->json_format(self::$path, "Testing the Json format for view my profile");
    }

when I run ./vendor/bin/codecept run api -vvv The error That I get is :Undefined variable: scenario, which is caused by this line of the code:

$I= new \ApiTester\generalSteps($scenario);

This is the full error that I get :

[ErrorException] Undefined variable: scenario

0 /Applications/MAMP/htdocs/larave-codeception/vendor/phpunit/phpunit/src/Framework/TestCase.php(693):

PHPUnit_Framework_TestResult->run(Object(Codeception\TestCase\Cest))

1 /Applications/MAMP/htdocs/larave-codeception/vendor/phpunit/phpunit/src/Framework/TestSuite.php(716):

PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))

2 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php(100):

PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))

3 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/SuiteManager.php(153):

Codeception\PHPUnit\Runner->doEnhancedRun(Object(PHPUnit_Framework_TestSuite), Object(PHPUnit_Framework_TestResult), Array)

4 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/Codecept.php(166):

Codeception\SuiteManager->run(Object(Codeception\PHPUnit\Runner), Object(PHPUnit_Framework_TestResult), Array)

5 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/Codecept.php(149):

Codeception\Codecept->runSuite(Array, 'api', NULL)

6 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/Command/Run.php(262):

Codeception\Codecept->run('api')

7 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/src/Codeception/Command/Run.php(191):

Codeception\Command\Run->runSuites(Array, Array)

8 /Applications/MAMP/htdocs/larave-codeception/vendor/symfony/console/Symfony/Component/Console/Command/Command.php(253):

Codeception\Command\Run->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

9 /Applications/MAMP/htdocs/larave-codeception/vendor/symfony/console/Symfony/Component/Console/Application.php(889):

Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

10 /Applications/MAMP/htdocs/larave-codeception/vendor/symfony/console/Symfony/Component/Console/Application.php(193):

Symfony\Component\Console\Application->doRunCommand(Object(Codeception\Command\Run), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

11 /Applications/MAMP/htdocs/larave-codeception/vendor/symfony/console/Symfony/Component/Console/Application.php(124):

Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

12 /Applications/MAMP/htdocs/larave-codeception/vendor/codeception/codeception/codecept(27):

Symfony\Component\Console\Application->run()

1

1 Answers

2
votes

you need the $scenario variable. You can get it if you add a second parameter to the function call of your CEST. It's automatically given to your test method, if you add them in the param list.

public function correct_json_format(ApiTester $I, $scenario) {
  $I= new \ApiTester\generalSteps($scenario);
  $I->json_format(self::$path, "Testing the Json format for view my profile");
}