Laravel 4-单元测试 [英] Laravel 4 - Unit Tests

查看:98
本文介绍了Laravel 4-单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write unit tests for my application, I think I know how to test a GET request for example; The controller I am testing has the following function, which is supposed to get the 'account create view'

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

此外,在这样的路由文件中引用了此

Also, this is referenced in the routes file like this

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

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

and what I am doing for testing looks like this:

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

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

  $this->assertResponseOk();
}

现在该测试通过了,但是如果我想测试POST请求怎么办? 我要测试的帖子功能如下:

Now this test passes, but what if I want to test the POST request? The post function I want to test is the following:

    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.');
        }
    }
}

在路由文件中也引用了以下内容:

This is also referenced in routes file like this:

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

我尝试编写以下测试,但未成功.我不确定出什么问题了,我想是因为这需要数据并且我没有正确传递它们?感谢您开始使用单元测试的任何线索.

I have tried to write the following test but no success. I am not sure what is going wrong, I think its because this needs data and i am not passing them in correctly? Any clue to get started with unit tests, appreciated.

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

推荐答案

确保在setUp函数中启用了路由过滤器和会话:

Make sure that the route filters and sessions are enabled in the setUp function :

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('/');
}

这篇关于Laravel 4-单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆