gpt4 book ai didi

testing - Laravel 4 - 单元测试

转载 作者:行者123 更新时间:2023-11-28 20:18:23 25 4
gpt4 key购买 nike

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

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 请求怎么办?我要测试的发布功能如下:

    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

最佳答案

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

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

关于testing - Laravel 4 - 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22482943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com