LoginSignup
0
0

More than 5 years have passed since last update.

codeception request 単体テスト

Last updated at Posted at 2017-07-25

リクエストを送る

$this->getModule('Laravel5')->_request('POST', '/api/v1/users', ['name' => $name]);

レスポンスを得る

$this->getModule('Laravel5')->_getResponseContent()

ページを開く

$this->getModule('Laravel5')->_loadPage('POST', '/checkout/step2', ['order' => $orderId]);

もっと詳しい:http://codeception.com/docs/modules/PhpBrowser

追加,POST例:


public function testAdd()
    {
        $this->laravelModule->_loadPage('GET', '/admin/useradd');
        //sentinelロールを取る(データベースから)
        $roles = Sentinel::getRoleRepository()->all();
        $content = $this->laravelModule->_getResponseContent();
        $this->assertContains('お名前', $content);
        $this->assertContains('部署名', $content);
        $this->assertContains('E-mail', $content);
        $this->assertContains('パスワード', $content);
         foreach ($roles as $role) {
            $this->assertContains($role->slug, $content);
        }
 }

作成,POST例:


    public function testStore()
    {
        $this->userService = $this->tester->grabService(UserService::class);
        $department = DB::table('departments')->first();
        $role = DB::table('roles')->first();
        $request = [
            'department_id' => $department->id,
            'first_name' => 'テスト',
            'last_name' => '太郎',
            'password' => '12345678',
            'email' => 'test@test.com',
            'user_new_role_' . $role->id => 'on',
        ];
        $user = $this->userService->create($request);
        $this->tester->seeRecord('users', [
            'department_id' => $user->department_id,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'email' => $user->email,
        ]);
        $this->userService->setRole($request, $user);
        $this->tester->seeRecord('role_users', [
            'user_id' => $user->id,
            'role_id' => $role->id,
        ]);
    }

削除、POST例:

$this->laravelModule = $this->getModule('Laravel5');
...
...
public function testDestroy()
{
    //adminページ入ったことを確認
    $this->laravelModule->_loadPage('GET', '/admin/users');
    $content = $this->laravelModule->_getResponseContent();
    $this->assertContains('システム', $content);

  //Post削除で確認
    $this->laravelModule->_loadPage('POST', '/admin/users/1', [
            '_token' => csrf_token(),
            '_method' => 'DELETE',
        ]);
    $this->laravelModule->_loadPage('GET', '/admin/users');
    $content = $this->laravelModule->_getResponseContent();
    $this->assertNotContains('システム', $content);
}

そのた、sentinelなど、shouldReceive()を使う

 onceを付けているので必ず1回だけ呼ばれないとエラーになります。またwithで引数の値も厳格に見てるので違うものが渡された場合はエラーになります。引数が何でも良い場合はwithAnyArgsなどを使います。

\Mail::shouldReceive('send')->andReturn(true);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0