Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 如何用mocky测试使用热情模型的控制器?_Testing_Laravel_Mocking_Mockery_Ardent - Fatal编程技术网

Testing 如何用mocky测试使用热情模型的控制器?

Testing 如何用mocky测试使用热情模型的控制器?,testing,laravel,mocking,mockery,ardent,Testing,Laravel,Mocking,Mockery,Ardent,我正在尝试在Laravel控制器中测试此功能: /** * Store a newly created resource in storage. * * @return Response */ public function store() { $project = $this->project->create(Input::all()); if ( $errors = $project->

我正在尝试在Laravel控制器中测试此功能:

/**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $project = $this->project->create(Input::all());
        if ( $errors = $project->errors()->all() ) {
           return Redirect::route('projects.create')
           ->withInput()
           ->withErrors($errors);

       }
       return Redirect::route('projects.index')
       ->with('flash', Lang::get('projects.project_created'));

   }
我的(错误)测试如下所示:

public function testStoreFails()
    {
        $this->mock->shouldReceive('create')
            ->once()
            ->andReturn(Mockery::mock(array(
                    false,
                    'errors' => array()
                    )));
        $this->call('POST', 'projects');
        $this->assertRedirectedToRoute('projects.create');
        $this->assertSessionHasErrors();
    }
问题是,基本上,当验证失败时,Ardent会将$project设置为false,但它也会在同一个对象上设置错误,可以使用->errors()->all()检索这些错误

在模拟游戏中,我很难找到要返回的内容

注意:构造函数注入模型:

public function __construct(Project $project)
    {
        $this->project = $project;

        $this->beforeFilter('auth');
    }

–由以下回答中的注释编辑–

如果我理解您正试图正确执行的操作,您可以这样做:

  • 模拟
    create()
    方法以返回模拟的
    项目
  • 模拟
    save()
    方法以返回false
  • 模拟一个实例,它是
    errors()
    将返回的对象。此模拟应具有模拟的
    all()
    方法
  • 使用mock
    errors()
    方法返回
    MessageBag
    mock
假设
$this->mock
项目
类的模拟:

// given that $this->mock = Mockery::mock('Project')

public function testStoreFails()
{
    // Mock the create() method to return itself
    $this->mock->shouldReceive('save')->once()->andReturn(false); 

    // Mock MessageBag and all() method
    $errors = Mockery::mock('Illuminate\Support\MessageBag');
    $errors->shouldReceive('all')->once()->andReturn(array('foo' => 'bar'));

    // Mock errors() method from model and make it return the MessageBag mock
    $this->mock->shouldReceive('errors')->andReturn($errors);

    // Proceed to make the post call
    $this->call('POST', 'projects');
    $this->assertRedirectedToRoute('projects.create');
    $this->assertSessionHasErrors();
}

PHP致命错误:在第43行的/dumminvoicing/app/controllers/projectscocontroller.PHP中对非对象调用成员函数errors(),我想create方法不能返回false,因为在这种情况下不能调用errors方法。好的,这是有意义的。我在你的问题中读到它被返回为假,我感到困惑。尝试编辑答案,我已将
create()
mock移到函数顶部,并更改了返回值。我还删除了模拟
错误时的
once()
,因为我认为
withErrors()
方法可能会再次调用它。。。以防万一。我得到了同样的错误:It’谢谢你的帮助,我想我们至少更接近了。也许它调用错误太早了,或者类似的东西?也许测试仍然以错误的方式编写,但是,在Ardent的代码中,没有任何“create”方法,我在他们的文档中也找不到任何引用。也许所有的代码都应该使用“save”方法运行。