Mock controller in Symfony2 unit test

Jlappano

I am writing unit tests for an API Service in a Symfony2 project. One service method takes a controller instance as an argument, and handles the requested JSON.

    public function getJSONContent(Controller $controller) {
        $version = $this->getAPIVersion();

        //Read in request content
        $content = $controller->get("request")->getContent();
        if (empty($content)) {
            throw new HttpException(400, 'Empty request payload');
        }


        //Parse and Detect invalid JSON
        $jsonContent = json_decode($content, true);
        if($jsonContent === null) {
            throw new HttpException(400, 'Malformed JSON content received');
        }

        return $jsonContent;
    }

The following is my test:

class ApiTest extends \PHPUnit_Framework_TestCase {
    public function testGetJSONContent() {

        // Create a stub for the OrgController Object
        $stub = $this->getMock('OrganizationController');

        // Create the test JSON Content
        $post = 'Testy Test';
        $request = $post;
        $version = "VersionTest";
        $APIService = new APIService();

        // Configure the Stub to respond to the get and getContent methods
        $stub->expects($this->any())
             ->method('get')
             ->will($this->returnValue($post));

        $stub->expects($this->any())
             ->method('getContent')
             ->will($this->returnValue($request));

        $stub->expects($this->any())
             ->method('getAPIVersion')
             ->will($this->returnValue($version));


        $this->assertEquals('Testy Test', $APIService->getJSONContent($stub));
    }
}

My test throws the following error:

Argument 1 passed to Main\EntityBundle\Service\APIService::getJSONContent() must be an instance of Symfony\Bundle\FrameworkBundle\Controller\Controller, instance of Mock_OrganizationController_767eac0e given.

My stub is obviously not fooling anyone, is there any way to fix this?

Alberto Gaona

Use the namespace to specify the controller you are mocking. I.e.

    // Create a stub for the OrgController Object
    $stub = $this->getMock('Acme\AcmeBundle\Controller\OrganizationController');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mock controller in Symfony2 unit test

From Dev

Is it possible to mock a controller and have access to the FormTagLib in the same unit test?

From Dev

Mock a controller for unit test without any service reference C#

From Dev

Unit Test Mock Controller, C# Do I need to Mock HTTPContext? What methods do I mock?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Unit Test Web Api 2 Mock User

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

PHPUnit test case for controller symfony2

From Dev

Mock IMemoryCache in unit test

From Dev

Unit test of controller angularjs

From Dev

Unit test angularjs controller

From Dev

angular unit test - controller

From Dev

Mock/Stub a RuntimeException in unit test

From Dev

Spock Mock not working for unit test

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Unit test a void method with Mock?

From Dev

how to mock a scope from a directive instead of a controller in angular unit-test

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

How do I mock controller context in my unit test so that my partial view to string function works?

From Dev

AngularJS controller unit test with Jasmine

From Dev

Perform unit test on a Spring controller

From Dev

Stuck with moq and unit test for Controller

From Dev

How do I unit test a controller in play framework 2 scala

From Dev

Unit test command class with mock repository

From Dev

How to android unit test and mock a static method

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

how to mock $window to unit test AngularJS service?

From Java

Using Moq to mock an asynchronous method for a unit test

Related Related

  1. 1

    Mock controller in Symfony2 unit test

  2. 2

    Is it possible to mock a controller and have access to the FormTagLib in the same unit test?

  3. 3

    Mock a controller for unit test without any service reference C#

  4. 4

    Unit Test Mock Controller, C# Do I need to Mock HTTPContext? What methods do I mock?

  5. 5

    How to mock AngularFire 2 service in unit test?

  6. 6

    Unit Test Web Api 2 Mock User

  7. 7

    How to mock AngularFire 2 service in unit test?

  8. 8

    PHPUnit test case for controller symfony2

  9. 9

    Mock IMemoryCache in unit test

  10. 10

    Unit test of controller angularjs

  11. 11

    Unit test angularjs controller

  12. 12

    angular unit test - controller

  13. 13

    Mock/Stub a RuntimeException in unit test

  14. 14

    Spock Mock not working for unit test

  15. 15

    How to mock springSecurityService in an unit test

  16. 16

    How to mock this unit test in Python?

  17. 17

    Unit test a void method with Mock?

  18. 18

    how to mock a scope from a directive instead of a controller in angular unit-test

  19. 19

    How do I mock controller context in my unit test so that my partial view to string function works?

  20. 20

    How do I mock controller context in my unit test so that my partial view to string function works?

  21. 21

    AngularJS controller unit test with Jasmine

  22. 22

    Perform unit test on a Spring controller

  23. 23

    Stuck with moq and unit test for Controller

  24. 24

    How do I unit test a controller in play framework 2 scala

  25. 25

    Unit test command class with mock repository

  26. 26

    How to android unit test and mock a static method

  27. 27

    Correct way to mock an AngularJS Service in a unit test?

  28. 28

    how to mock $window to unit test AngularJS service?

  29. 29

    Using Moq to mock an asynchronous method for a unit test

HotTag

Archive