简体   繁体   English

使用phpunit和Mockery模拟Laravel 5.2本地范围

[英]Mocking Laravel 5.2 local scope with phpunit and Mockery

I'm using Laravel 5.2, phpunit 5.0.0 and PHP 7.0.3 and try to write a test with database interaction that touches an Eloquent models scope method. 我正在使用Laravel 5.2,phpunit 5.0.0和PHP 7.0.3,并尝试使用数据库交互编写测试,该测试涉及Eloquent模型范围方法。

I have a something like that: 我有类似的东西:

class Picture extends Illuminate\Database\Eloquent\Model {
    ...
    public function scopeGetPictureNameById($oQuery, $pictureHId) {
         return $oQuery->select('name')->where('h_id', '=',   $pictureHId)->first()->name;
    }
}

class someHelperClass {
    public function someMethod($pictureId) {
        $pictureName = Picture::getPictureNameById($pictureId);
        return "name is " . $pictureName;
    }
}


class SomeTest extends TestCase {

    use DatabaseMigrations;

    protected $someHelper;

    public function setUp() {
        parent::setUp();
        $this->someHelper = new SomeHelper();
    }

    /**
     * @test
     */
    public function someMethodTest() {
        $expectedName = "test";
        $this->assertEquals("name is " . $expectedName, $this->someHelper->someMethod());
    }
}

I seed the Database with a Picture record where the name is set to "test". 我使用Picture记录为数据库播种,其中名称设置为“test”。

The first thing I thought was that I would not have to mock the scope call, because all I need is in the Database. 我想的第一件事是我不必模拟范围调用,因为我需要的只是在数据库中。 And since the (non simplified) code I have works outside the test, I guess that scope calls don't work in phpunit. 并且由于(非简化的)代码我在测试之外工作,我猜范围调用在phpunit中不起作用。 (I get a "Trying to get property of non-object" Exception). (我得到了“试图获取非对象属性”的例外)。

Okay, so I tried to mock the call with Mockery: 好的,所以我试着用Mockery来模拟这个电话:

class SomeTest extends TestCase {

    use DatabaseMigrations;

    protected $someHelper;

    public function setUp() {
        parent::setUp();
        $this->someHelper = new SomeHelper();
    }

    /**
     * @test
     */
    public function someMethodTest() {
        $expectedName = "test";

        $mockedPicture = Mockery::mock('overload:App\Models\Picture');
        $mockedPicture->shouldReceive('getPictureNameById')->andReturn('test');

        //also tried this: $mockedPicture->shouldReceive('scopeGetPictureNameById')->andReturn('test');


        $this->assertEquals("name is " . $expectedName, $this->someHelper->someMethod());
    }
}

All I get is the "Could not load mock App\\Models\\Picture, class already exists". 我得到的是“无法加载模拟App \\ Models \\ Picture,类已经存在”。 So how can I properly mock query scope calls like Picture::getPictureNameById($pictureId)? 那么我怎样才能正确模拟查询范围调用,如Picture :: getPictureNameById($ pictureId)?

I would use dependency injection instead of calling methods on the Picture class statically. 我会使用依赖注入而不是静态地调用Picture类上的方法。 So something like this: 所以像这样:

class someHelperClass {

    protected $picture;

    public function __construct(Picture $picture) {
        $this->picture = $picture;
    }

    public function someMethod($pictureId) {
        $pictureName = $this->picture->getPictureNameById($pictureId);
        return "name is " . $pictureName;
    }
}

Then in your test: 然后在你的测试中:

public function someMethodTest() {
    $expectedName = "test";

    $mockedPicture = Mockery::mock('App\Models\Picture');
    $mockedPicture->shouldReceive('getPictureNameById')->andReturn('test');
    $someHelper = new SomeHelper($mockedPicture);

    $this->assertEquals("name is " . $expectedName, $someHelper->someMethod(1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM