mercredi 1 juin 2016

Mocking Laravel 4.2 Facades with Code Ception

I've recently taken over a legacy project which has been built on Laravel 4.2. Currently there's minimal tests which and I'm currently implementing more tests as we speak!

I'm trying to unit test a method that calls the \Illuminate\Support\Facades\DB facade but I'm struggling to mock the it.

Here's what I've tried so far:

tests/unit.suite.yml:

class_name: UnitTester
modules:
    enabled:
        - Asserts
        - Laravel4
        - \Helper\Unit

tests\unit\TestCase.php:

namespace tests\unit;

/**
 * Class TestCase
 *
 * @package tests\unit
 */
abstract class TestCase extends \Codeception\TestCase\Test
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    /**
     * The class been tested
     *
     * @var object
     */
    protected $testClass;

    /**
     * Teardown
     */
    protected function _after()
    {
        \Mockery::close();
    }
}

tests/unit/MyTest.php:

namespace tests\unit\models;

use Illuminate\Support\Facades\DB;
use \Mockery as m;
use tests\unit\TestCase;

/**
 * Class MyTest
 *
 * @package tests\unit\models
 */
class MyTest extends TestCase
{
    /**
     * Test retrieving a user by token
     *
     * @todo Uncomment this when we're able to mock facades
     */
    public function testRetrieveByToken()
    {
        $userMock = m::mock('\User');

        DB::shouldRecieve('table')->once();

        $this->assertSame(
            $userMock,
            $this->testClass->retrieveByToken('username', 'T0k3n')
        );
    }
}

When running this particular test, I get the following error:

[ErrorException] call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'shouldRecieve'

Aucun commentaire:

Enregistrer un commentaire