【问题标题】:Laravel 4 - Unit TestsLaravel 4 - 单元测试
【发布时间】:2014-03-18 14:52:33
【问题描述】:

我正在尝试为我的应用程序编写单元测试,例如,我想我知道如何测试 GET 请求; 我正在测试的控制器具有以下功能,应该可以获取“帐户创建视图”

public function getCreate() {
    return View::make('account.create');
}

另外,在路由文件中也有这样的引用

/*
Create account (GET)
*/
Route::get('/account/create', array(
    'as'  =>  'account-create',
    'uses'  =>  'AccountController@getCreate'
));

我正在做的测试看起来像这样:

public function testGetAccountCreate()
{ 
  $response = $this->call('GET', '/account/create');

  $this->assertTrue($response->isOk()); 

  $this->assertResponseOk();
}

现在这个测试通过了,但是如果我想测试 POST 请求呢? 我要测试的post函数如下:

    public function postCreate() {
    $validator = Validator::make(Input::all(),
        array(
            'email'           => 'required|max:50|email|unique:users',
            'username'        => 'required|max:20|min:3|unique:users',
            'password'        => 'required|min:6',
            'password_again'  => 'required|same:password'
        )
    );

    if ($validator->fails()) {
        return Redirect::route('account-create')
                ->withErrors($validator)
                ->withInput();
    } else {

        $email    = Input::get('email');
        $username = Input::get('username');
        $password = Input::get('password');

        // Activation code
        $code     = str_random(60);

        $user = User::create(array(
            'email' => $email,
            'username' => $username,
            'password' => Hash::make($password),
            'password_temp' => '',
            'code'  => $code,
            'active'  => 0
        ));

        if($user) {

            Mail::send('emails.auth.activate', array('link' => URL::route('account-activate', $code), 'username' => $username), function($message) use ($user) {
                $message->to($user->email, $user->username)->subject('Activate your account');
            });

            return Redirect::route('account-sign-in')
                  ->with('global', 'Your account has been created! We have sent you an email to activate your account.');
        }
    }
}

这在路由文件中也有这样的引用:

  /*
  Create account (POST)
  */
  Route::post('/account/create', array(
      'as'  =>  'account-create-post',
      'uses'  =>  'AccountController@postCreate'
  ));

我尝试编写以下测试但没有成功。我不确定出了什么问题,我认为是因为这需要数据而我没有正确传递它们?任何开始进行单元测试的线索,不胜感激。

public function testPostAccountCreate()
{
$response = $this->call('POST', '/account/create');
$this->assertSessionHas('email');
$this->assertSessionHas('username');
$this->assertSessionHas('email');
}

测试的输出是:

There was 1 failure:

1) AssetControllerTest::testPostAccountCreate
Session missing key: email
Failed asserting that false is true.

/www/assetlibr/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:271
/www/assetlibr/app/tests/controllers/AssetControllerTest.php:23

【问题讨论】:

    标签: testing laravel laravel-4


    【解决方案1】:

    确保在设置功能中启用了路由过滤器和会话:

    public function setUp()
    {
        parent::setUp();
    
        Session::start();
    
        // Enable filters
        Route::enableFilters();
    }  
    

    例如测试登录:

    public function testShouldDoLogin()
    {
    // provide post input
    
    $credentials = array(
            'email'=>'admin',
            'password'=>'admin',
            'csrf_token' => csrf_token()
    );
    
    $response = $this->action('POST', 'UserController@postLogin', null, $credentials); 
    
    // if success user should be redirected to homepage
    $this->assertRedirectedTo('/');
    }
    

    【讨论】:

    • 我修改了它并且工作了谢谢!但是我没有包含设置功能,因为它会给我一个错误。我得到的错误破坏了我的测试并且不会运行任何
    【解决方案2】:

    您实际上并没有说不成功是什么样子,但如果是您的断言失败,那将是因为您的代码没有将任何数据放入会话中,因此即使您是,这些断言也会失败通过您的发布请求传递数据。

    要传递该数据,您需要在 call() 方法中添加第三个参数,如下所示:

    public function testPost()
    {
        $this->call('POST', 'account/create', ['email' => 'foo@bar.com']);
    
        $this->assertResponseOk();
    
        $this->assertEquals('foo@bar.com', Input::get('email'));
    }
    

    尽管在实践中,我建议您测试适当的结果,即。根据输入数据重定向和发送邮件,而不是检查传递的数据。

    【讨论】:

    • 谢谢,很抱歉没有包括测试的输出。现在包括在内。
    • 这很有用并且符合我的怀疑,即失败是因为测试在 Session 中查找数据,除非你特意把它放在那里,否则它不会找到它。
    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2016-08-26
    • 2020-09-03
    • 2017-10-26
    • 2021-11-16
    相关资源
    最近更新 更多