Marcel Scherf Marcel Scherf -4 years ago 334
reST (reStructuredText) Question

How to save a JSON-response from a REST-client in Codeception without using PhpBrower

I'm trying to test my own Rest-API using a Client-library which handles the requests for me.
I've written a helper in which the Client gets initialized and a action is added called "wantToGetUserOverClient($userId)". The client does the request and returns a JSON, that works as intended when I var_export() the JSON.
I've tried to extend the Rest module and assigning the public response field directly but get an exception:

[ModuleException] PhpBrowser: Page not loaded. Use `$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it


My Question:
What is the best way to save the Client response in the Codeception REST module response so I can use the already existing JSON-actions.
For example:

$I->wantToGetUserOverClient(1234);
$I->canSeeResponseIsJson();


without loading a Page withe the PhpBrower.

Thanks in advance.

My helper:

class ApiHelper extends \Codeception\Module\REST
{
private $backendApi;

public function __construct(ModuleContainer $moduleContainer, $config = null)
{
parent::__construct($moduleContainer, $config);
$this->backendApi = new BackendRestAPI();
}

public function wantToGetUserOverClient($userId)
{
$this->response = $this->backendApi->user()->one($userId);
}
}

Answer Source

You are misusing REST module.
But if you insist, you have to replace PhpBrowser with your module.
Configuration:

REST:
  depends: YourModule

To make seeResponseIsJson work, YourModule must extend Codeception\Lib\InnerBrowser and implement _getResponseContent method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download