【问题标题】:Laravel Controller UNIT Testing Issue?Laravel 控制器单元测试问题?
【发布时间】:2013-05-28 10:07:50
【问题描述】:

我正在创建一个 Laravel 4 应用程序,但遇到了一些问题。在为我的控制器编写测试时,我注意到出于某种奇怪的原因,它似乎永远无法验证。这是我的(精简的)控制器代码。

<?php use Controllers\Base\PublicController;

class GuestController extends PublicController {

    /**
     * Display the report issue form.
     *
     * @return null
     */
    public function getReportIssue()
    {
        $this->layout->title = Lang::get('app.report_issue');

        $this->layout->content = View::make('guest.report_issue');
    }

    public function postReportIssue()
    {
        $rules = [
            'full_name' => 'required|min:2|max:100',
            'email'     => 'required|email',
            'issue'     => 'required|min:10|max:1000',
        ];

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails())
        {
            return Redirect::route('guest.report_issue')
                ->withInput()
                ->withErrors($validator->messages());
        }

        return Redirect::route('guest.reported_issue')
            ->with('msg', 'Okay');
    }
}

现在他为上述两种方法创建的测试是......

public function testHandleFailReportIssue()
{
    Input::replace([
        'full_name' => '',
        'email'     => '',
        'issue'     => '',
    ]);

    $this->call('POST', 'report-issue');

    $this->assertRedirectedToRoute('guest.report_issue');

    $this->assertSessionHasErrors(['full_name', 'email', 'issue']);
}

public function testHandlePassReportIssue()
{
    Input::replace([
        'full_name' => 'John Doe',
        'email'     => 'john@localhost.dev',
        'issue'     => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
                        Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar'
    ]);

    $this->call('POST', 'report-issue');

    $this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}

第一次测试成功通过,但第二次失败。经过一番调查,它显示验证没有通过,这意味着Input::replace() 方法没有完成它的工作,因为我注入了有效的请求值。也许我错过了什么?

[编辑]

我决定这样做

public function testHandlePassReportIssue()
{
    Input::replace([
        'full_name' => 'John Doe',
        'email'     => 'john@flashdp.com',
        'issue'     => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
                        Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar',
    ]);

    $response = $this->route('POST', 'guest.report_issue');

    dd($this->app['session']->get('errors'));

    $this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}

在通过检查会话进行调试的测试中,就像我怀疑的那样,输入没有被填充,这可能是什么原因?验证消息已返回。

【问题讨论】:

    标签: php unit-testing laravel laravel-4


    【解决方案1】:
    $response = $this->route('POST', 'guest.report_issue', array(
        'full_name' => 'Foo',
        'email' => 'Man@Chu.com',
        'issue' => 'FooBar'));
    

    您可以将参数作为数组传递。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2018-03-09
      • 2014-09-13
      • 2021-02-17
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2014-02-14
      相关资源
      最近更新 更多