【问题标题】:PHPunit: testing a RepositoryPHPunit:测试存储库
【发布时间】:2016-07-28 00:24:17
【问题描述】:

我在 Laravel 中测试我的存储库,遇到了一些问题,很可能与我的方法结构有关。

所以,我的存储库看起来像:

<?php

namespace Repositories\User;

use App\Test\Models\Entities\User;
use Illuminate\Database\Eloquent\Model;

class UserRepository implements UserInterface
{

    /**
     * @var Model $userModel
     */
    protected $userModel;

    /**
     * Setting our class $userModel to the injected model
     *
     * @param Model $userModel
     * @return UserRepository
     */
    public function __construct(Model $userModel)
    {
        $this->userModel = $userModel;
    }


    /**
     * Returns the User object associated with the userEmail
     *
     * @param string $userEmail
     * @return User | null
     */
    public function getUserByEmail($userEmail)
    {
        // Search by email
        $user = $this->userModel
                        ->where('email', '=', strtolower($userEmail))
                        ->first();

        if ($user) {
            return $user->first();
        }

        return null;
    }



}


 /**
     * @param $id
     * @param $email
     * @param $source
     *
     * @dataProvider usersDataProvider
     */
    public function testGetUserByEmail($id, $email, $source)
    {
        $user = new User();
        $user->id = $id;
        $user->email = $email;
        $user->user_source_id = $source;

        $this->user->shouldReceive('getUserByEmail')->once()
            ->andReturn($user);

    }

我是使用 Mockery 的新手,我只是想知道我是否遵循正确的方法来测试我的 getUserByEmail($email) 方法。请记住(如预期的那样)getUserByEmail($email) 会调用数据库。

另外,这是我收到的消息:

PHP Fatal error: Call to a member function connection() on null in /private/var/www/ff-php-prelaunch/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php,这可能意味着没有与数据库的初始化连接。

更新

顺便说一句,我的setUp()如下:

  public function setUp()
    {
        $this->user = Mockery::mock('App\Test\Models\Entities\User');

        parent::setUp();
    }

【问题讨论】:

    标签: unit-testing laravel phpunit mockery


    【解决方案1】:

    你可能没有扩展 Laravel 在 tests 目录中提供的默认 TestCase 类。您的环境不正确,您没有连接到数据库。

    如果您不想实际查询数据库,您应该使用Mockery 来创建依赖项的模拟(此处为 $userModel)。你基本上按照下面的方式创建一个Mock,我没有测试这个代码但是大致的想法就在这里。

    protected function setUp() {
        parent::setUp();
    
        $userModelMockedMethods = [
            'where' => 'some return'
        ];
        // This is our dependency mock
        $userModelMock = Mockery::mock(Model::class, $userModelMockedMethods);
        // this mock now will return 'some return' if you call the `where` method 
        // on it. If you wish the where method to return something callable, you
        // should return another mock instead of a string
    
        // This replaces the mock in the dependency manager.
        $this->app->instance(Model::class, $UserModelMock);
    }
    

    【讨论】:

    • 这是正确的,因为我是从PHPUnit_Framework_TestCase 扩展而来的。但是,在这种情况下,我必须添加createApplication(),这是默认需要的。
    • 如果你看一下默认的 TestCase,它有一个 createApplication 但不是从 PHPUnit 的 TestCase 派生的,而是从 Laravel 类派生的。
    • 感谢您的提示,@AsTeR。但是,如果我模拟这些方法,我将在测试中断言什么?老实说,在这一点上我有点困惑。
    • @thitami 通常单元测试旨在将被测组件与其他组件隔离开来。我从来没有遇到过模拟数据库的情况,因为这很麻烦,而且我并没有真正获得附加值。在那个具体的情况下,我建议不要这样做,但既然你问了这个问题,那么答案就在其中。
    • 好的,太好了。我会听从您的建议,不再测试数据库数据。我已经稍微重构了我的代码,尤其是setUp()。但是,我现在收到:Fatal error: Call to a member function instance() on null。有什么想法吗?
    猜你喜欢
    • 2017-08-16
    • 2011-07-15
    • 2015-06-15
    • 2011-05-08
    • 2013-01-20
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多