Unit testing Laravel队列::shouldReceive()
我的一条路线上有以下内容Unit testing Laravel队列::shouldReceive(),unit-testing,laravel,laravel-4,mockery,Unit Testing,Laravel,Laravel 4,Mockery,我的一条路线上有以下内容 $rules = array( 'name' => 'Required', 'subject' => 'Required', 'message' => 'Required', 'email' => 'Required|Email', 'recaptcha_response_field' => 'required|recaptcha' ); $validator = Validator::make(I
$rules = array(
'name' => 'Required',
'subject' => 'Required',
'message' => 'Required',
'email' => 'Required|Email',
'recaptcha_response_field' => 'required|recaptcha'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::to('contact')->withInput()->withErrors($validator);
}else{
$data = array('name' => Input::get('name'),
'email' => Input::get('email'),
'text' => Input::get('message'),
'subject' => Input::get('subject'));
Queue::push('ContactQueue', $data);
return Redirect::to('contact')->with('success', 'Message sent successfully');
}
我正在尝试为成功场景编写单元测试,我有以下内容:
public function testSuccess(){
Validator::shouldReceive('make')->once()->andReturn(Mockery::mock(['fails' => false]));
Queue::shouldReceive('push')->once();
$this->call('POST', '/contact');
$this->assertRedirectedTo('/contact');
}
但在尝试运行phpunit时,我一直收到以下错误:
BadMethodCallException:Method Illumb\Queue\QueueManager::connected()在此模拟对象上不存在
有什么想法吗?放入
队列::shouldReceive('connected')->once()代码>
在Queue::shouldReceive('push')->once()之后代码>
解决了这个问题。进一步阐述,即使您使用Queue::shouldReceive('push')->never()代码>您仍然需要代码中的其他行。也许Laravel总是在不需要的时候启动队列驱动程序,所以您需要模拟它。