Laravel Queue :: shouldReceive()

时间:2014-01-13 21:13:16

标签: 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(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 Illuminate\Queue\QueueManager::connected() does not exist on this mock object

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

Queue::shouldReceive('connected')->once();Queue::shouldReceive('push')->once();之后 解决了这个问题。