0

我正在使用laravel (4.2)框架来开发 Web 应用程序 (PHP 5.4.25)。我创建了一个使用 eloquent-repository 实现的存储库接口,我在 UserController 中使用该存储库:

#   app/controllers/UsersController.php

use Gas\Storage\User\UserRepositoryInterface as User;

class UsersController extends \BaseController {
    protected $user;

    public function __construct(User $user) {
        $this->user = $user;

    }

    public function store() {
        $input = Input::all();
        $validator = Validator::make(Input::all(), $this->user->getRoles());

        if ( $validator->passes() ) {
            $this->user->getUser()->username = Input::get('username');
            $this->user->getUser()->password = Hash::make(Input::get('password'));
            $this->user->getUser()->first_name = Input::get('first_name');
            $this->user->getUser()->last_name = Input::get('last_name');
            $this->user->getUser()->email = Input::get('email');
            $this->user->save();


            return false;
        } else {
            return true;
        }
    }
}

我的存储库实现:

namespace Gas\Storage\User;

#   app/lib/Gas/Storage/User/EloquentUserRepository.php

use User;

class EloquentUserRepository implements UserRepositoryInterface {

    public $_eloquentUser;

    public function __construct(User $user) {
        $this->_eloquentUser = $user;
    }

    public function all()
    {
        return User::all();
    }

    public function find($id)
    {
        return User::find($id);
    }

    public function create($input)
    {
        return User::create($input);
    }

    public function save()
    {
        $this->_eloquentUser->save();
    }

    public function getRoles()
    {
        return User::$rules;
    }

    public function getUser()
    {
        return $this->_eloquentUser;
    }
}

我还创建了一个 UsersControllerTest 来测试控制器,一切正常,用户已添加到数据库中。在我嘲笑了我的UserRepositoryInterface之后,因为我不需要测试数据库插入,但我只想测试控制器

class UsersControllerTest extends TestCase {
    private $mock;

    public function setUp() {
        parent::setUp();
    }

    public function tearDown() {
        Mockery::close();
    }

    public function mock($class) {
        $mock = Mockery::mock($class);

        $this->app->instance($class, $mock);

        return $mock;
    }

    public function testStore() {
        $this->mock = $this->mock('Gas\Storage\User\UserRepositoryInterface[save]');

        $this->mock
            ->shouldReceive('save')
            ->once();

        $data['username'] = 'xxxxxx';
        $data['first_name'] = 'xxxx';
        $data['last_name'] = 'xxxx';
        $data['email'] = 'prova@gmail.com';
        $data['password'] = 'password';
        $data['password_confirmation'] = 'password';

        $response = $this->call('POST', 'users', $data);

        var_dump($response->getContent());
    }
}

我的 ruote 文件:

Route::resource('users', 'UsersController');

当我运行测试时,我收到以下错误:

Mockery\Exception\InvalidCountException : Method save() from Mockery_0_Gas_Storage_User_UserRepositoryInterface should be called
 exactly 1 times but called 0 times.

为什么没有调用模拟方法save ?

怎么了?


编辑:没有部分模拟一切正常,现在的问题是:为什么部分模拟它不起作用?


谢谢

4

2 回答 2

2

回顾您的代码,您似乎应该能够通过将模拟函数更改为以下内容来使用部分模拟:

public function mock($class) {
    $mock = Mockery::mock($class);
    $ioc_binding = preg_replace('/\[.*\]/', '', $class);
    $this->app->instance($ioc_binding, $mock);

    return $mock;
}
于 2014-09-16T22:50:28.193 回答
1

您正在告诉模拟期望该save()方法,但它save()位于存储库内的 Eloquent 模型上,而不是您正在模拟的存储库。

您的代码目前正在泄漏存储库实现的详细信息。

而不是调用:

$this->user->getUser()->username = Input::get('username');

您需要将 的实例传递User到存储库中:

$this->user->add(User::create(Input::all());

或者您将数组传递Input到存储库并允许存储库在User内部创建一个新实例:

$this->user->add(Input::all());

然后,您将在测试中模拟该add()方法:

$this->mock->shouldReceive('add')->once();

关于 Laravel 不适合模拟或单元测试的评论是错误的。

于 2014-09-15T06:03:12.900 回答