Reputation: 3705
I am in the process of building and writing tests for a front-end app which is making calls to an API for all of its data. I am testing with Codeception. So far, functional and acceptance tests working however, I want functional tests to be independent of the API so that I can run them without depending on the API serving app.
Is there a way of mocking data coming from API calls? Or is this the domain of unit testing?
Upvotes: 11
Views: 2059
Reputation: 988
Check out Mocky (http://www.mocky.io) for live mock HTTP responses that you can customize. Also, a great tool for creating incredibly awesome stubbed JSON objects, check out JSON-Generator (http://www.json-generator.com/).
Upvotes: 0
Reputation: 2996
I have used PHPUnit for API testing. I hope this will help you.
I have just provided some sample input data for this test and validate that it will return error/success code as expected If test hasn't got the expected return code, then it will prompt an error.
class ForgotPasswordTest extends \TestCase{
/**
* Test Forgot Password API With valid parameters
* It will check forgot password functionality with success conditions
*/
public function testForgotPasswordWithValidParameter()
{
$param=array("email"=>"[email protected]");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 600 in case if API executed successfully
*/
$this->assertEquals("600", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check whether you have applied user exist condition
*/
public function testForgotPasswordWithInValidParameter()
{
$param=array("email"=>"[email protected]");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 404 if user not found
*/
$this->assertEquals("404", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check input validations are working fine in the API
*/
public function testForgotPasswordWithInValidEmail()
{
$param=array("email"=>"satish");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 400 error code if there are some input validation error
*/
$this->assertEquals("400", $data["code"]);
}
}
You can set some other test cases also, for that just need to create new function in this class with different test cases.
Upvotes: 1