11

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?

Adamski
  • 3,585
  • 5
  • 42
  • 78
  • This will probably depend on what REST library you use to make those API calls. Otherwise a framework and library agnostic solution would be to create a lightweight server process that mocks the API responses and use that server instead of the real API server when testing. –  Sep 20 '14 at 11:36
  • Take a look at [apiary.io](http://apiary.io/) It can be used for designing and mocking an API – lukasgeiter Dec 29 '14 at 19:15

2 Answers2

1

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"=>"shindesatishsss@gmail.com");
        $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"=>"test@test.com");
        $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.

Satish Shinde
  • 2,878
  • 1
  • 24
  • 41
0

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/).

mansilladev
  • 1,010
  • 8
  • 16