4

I tried to make a unit test for my custom module like this:

app/code/MyCompany/UnitTest/Test/Unit/Controller/SpinTest.php

namespace MyCompany\UnitTest\Controller;

class SpinTest extends \Magento\TestFramework\TestCase\AbstractController
{
    public function testLogin()
    {
        $params = [
          'customer_id' => '3'
        ];
        $this->getRequest()->setPostValue($params);
        $this->dispatch('spin/run');
        $result = $this->getResponse()->getBody();
        $this->assertEquals('200', $result['status']);
    }
}

And ran the unit from terminal:

vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/MyCompany/UnitTest/Test/Unit/Controller/

But i got an error like this:

Class 'Magento\TestFramework\TestCase\AbstractController' not found in /var/www/mage2/app/code/MyCompany/UnitTest/Test/Unit/Controller/SpinTest.php on line 4

3

1 Answer 1

-1

That class does not exist in Magento 2, so you got the error message. The \PHPUnit\Framework\TestCase class should be used for every TestCase in Magento 2.x.

So, your code should look like this:

namespace MyCompany\UnitTest\Controller;

class SpinTest extends \PHPUnit\Framework\TestCase
{
    public function testLogin()
    {
        $params = [
          'customer_id' => '3'
        ];
        $this->getRequest()->setPostValue($params);
        $this->dispatch('spin/run');
        $result = $this->getResponse()->getBody();
        $this->assertEquals('200', $result['status']);
    }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.