Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeception - mocking api call data

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?

like image 918
Adamski Avatar asked Apr 15 '14 14:04

Adamski


1 Answers

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.

like image 131
Satish Shinde Avatar answered Oct 24 '22 08:10

Satish Shinde