Я разрабатываю PHP (5.4.25) приложение с laravel (4.2) рамки. Я хотел бы проверить свою UserController с издевательством, поэтому я приспосабливать свою UserController таким образом:Laravel mock with Mockery Eloquent модели
class UsersController extends \BaseController {
protected $user;
public function __construct(User $user) {
$this->user = $user;
$this->beforeFilter('csrf', array('on'=>'post'));
}
public function store() {
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()) {
$this->user->username = Input::get('username');
$this->user->password = Hash::make(Input::get('password'));
$this->user->first_name = Input::get('first_name');
$this->user->last_name = Input::get('last_name');
$this->user->email = Input::get('email');
$this->user->save();
return true;
} else {
return false;
}
}
Я хочу, чтобы макет модели Eloquent пользователя, поэтому я развивать свою UsersControllerTest так:
class UsersControllerTest extends TestCase {
private $mock;
public function __construct() {}
public function setUp() {
parent::setUp();
$this->createApplication();
}
public function tearDown() {
parent::tearDown();
Mockery::close();
}
public function testStore() {
$this->mock = Mockery::mock('Eloquent','User[save]');
$this->mock
->shouldReceive('save')
->once()
->andReturn('true');
$this->app->instance('User', $this->mock);
$data['username'] = 'qwerety';
$data['first_name'] = 'asd';
$data['last_name'] = 'asd123';
$data['email'] = '[email protected]';
$data['password'] = 'password';
$data['password_confirmation'] = 'password';
$response = $this->call('POST', 'users', $data);
var_dump($response->getContent());
}
}
Когда я запускаю свой тест, он возвращает мне эту ошибку:
Mockery\Exception\InvalidCountException : Method save() from Mockery_0_User should be called
exactly 1 times but called 0 times.
Почему? Что не так?
EDIT: Я нашел проблему - Если я не использую фиктивный объект все отлично работает и контроллер создания нового пользователя в БД, но когда я использую макет метод Input:all()
возвращает пустой массив.
- Благодаря
Также удалив экземпляр нового класса в методе хранилища, который я понес в той же ошибке. – PapaSmurf