【问题标题】:Laravel Queue::shouldReceive()Laravel 队列::shouldReceive()
【发布时间】:2014-02-01 19:11:55
【问题描述】:

我的一条路线中有以下内容

$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

有什么想法吗?

【问题讨论】:

    标签: unit-testing laravel laravel-4 mockery


    【解决方案1】:

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

    【讨论】:

    • 更详细地说,即使您使用Queue::shouldReceive('push')->never();,您仍然需要在代码中的某处使用另一行。也许 Laravel 总是在不需要时启动 Queue 驱动程序,所以你需要模拟它。
    猜你喜欢
    • 2015-09-12
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2015-02-23
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多