问题模拟Laravel可启动模型特征

时间:2016-04-12 17:34:34

标签: php unit-testing laravel testing mockery

我使用bootable model trait使用我的Trait为模型注册某些事件。但是,我试图模拟使用该特征的模型时遇到了一个问题。具体来说,当模型的Mockery版本被实例化时,它的启动代码同意它应该有一个bootMyTrait方法,但在尝试调用它时无法找到它。

下面的

Sample Repository,带有重现命令。

举个例子,这是一个特点:

namespace App;
trait MyTrait
{
    public static function bootMyTrait()
    {
        print("Booting MyTrait\n");
    }
}

使用它的模型:

namespace App;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
    use MyTrait;
}

定期实例化模型正常。这显示了所需的输出:

$model = new MyModel();

然而,试图模仿这个模型并不合作。这样:

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;


    class ExampleTest extends TestCase
    {
        /**
         * A basic functional test example.
         *
         * @return void
         */
        public function testTraitBooting()
        {
            $model = $this->getMock('App\MyModel');
        }
    }


Fails. Adding some debugging to Eloquent:


    /**
     * Boot all of the bootable traits on the model.
     *
     * @return void
     */
    protected static function bootTraits()
    {
        $class = static::class;

        foreach (class_uses_recursive($class) as $trait) {
            print("\nTesting that class: $class has method: " . $method = 'boot'.class_basename($trait) . " because of Trait: $trait\n");
            if (method_exists($class, $method = 'boot'.class_basename($trait))) {
                print("Class: $class has method: $method \n");
                try {
                    forward_static_call([$class, $method]);
                } catch (\PHPUnit_Framework_MockObject_BadMethodCallException $e) {
                    print("Class: $class failed calling $method\n");
                    throw $e;
                }
            }
        }
    }

给我们这个失败:

PHPUnit 5.1.0 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)
Testing that class: Mock_MyModel_9ee820db has method: bootMyTrait because of Trait: App\MyTrait
Class: Mock_MyModel_9ee820db has method: bootMyTrait
Class: Mock_MyModel_9ee820db failed calling bootMyTrait


Time: 129 ms, Memory: 18.00Mb

There was 1 error:

1) ExampleTest::testTraitBooting
PHPUnit_Framework_MockObject_BadMethodCallException:

mock-bootable-laravel-model-trait/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:326
mock-bootable-laravel-model-trait/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:309
mock-bootable-laravel-model-trait/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:296
mock-bootable-laravel-model-trait/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:277
mock-bootable-laravel-model-trait/tests/ExampleTest.php:16

我也试过用几种不同的方式创建模拟。以DatabaseSoftDeletingTraitTest为例:

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Mockery as m;

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTraitBooting()
    {
        $mock = m::mock('App\MyModel');
        $mock->shouldReceive('bootMyTrait')->once();
    }
}

但是在这里,永远不会调用bootMyTrait:

PHPUnit 5.1.0 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 149 ms, Memory: 19.25Mb

There was 1 error:

1) ExampleTest::testTraitBooting
Mockery\Exception\InvalidCountException: Method bootMyTrait() from Mockery_0_App_MyModel should be called
 exactly 1 times but called 0 times.

mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery/CountValidator/Exact.php:37
mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery/Expectation.php:271
mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:120
mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery/Container.php:297
mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery/Container.php:282
mock-bootable-laravel-model-trait/vendor/mockery/mockery/library/Mockery.php:142
mock-bootable-laravel-model-trait/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:122

因此,我可以将我在启动方法中执行的代码移动到ServiceProvider,但随后我需要注册使用该特征的每个模型。这感觉很脏,使用启动方法似乎是合适的。所以我认为我要么犯了一个错误,要么错误地模拟了特质使用模型。我看过getMockForTrait但我还需要模拟实例来扩展Eloquent(一些特征&方法称为雄辩方法)

如果有人看到我错过的东西(或者我完全接近这个错误的方式),非常感谢

1 个答案:

答案 0 :(得分:2)

经过一些测试,我相信这种方法足以测试它:

$mock = m::mock('App\MyModel')->makePartial();
$mock->shouldReceive('bootMyTrait')->once();
$mock->__construct();

说明:

  1. $mock = m::mock('App\MyModel')->makePartial();

    我们创建mock但是我们将它部分化,因为我们想要使用默认的类构造函数和其他方法。使其偏袒意味着我们不会覆盖的所有方法都将使用原始App\MyModel

  2. $mock->shouldReceive('bootMyTrait')->once();

    这应该很明显 - 我们要验证bootMyTrait方法是否正好运行一次

  3. $mock->__construct();

    这样我们就可以运行默认的类构造函数了。在创建模拟时,似乎没有使用构造函数,因此我们无法以其他方式测试它。如果我们想确保启动原始类构造函数,我们需要手动启动对象构造函数方法。