【问题标题】:Testing with phpunit a try catch store method to test the exception用 phpunit 测试一个 try catch store 方法来测试异常
【发布时间】:2018-10-18 08:13:02
【问题描述】:

我想测试一下这个功能:

public function store(array $input){

        \DB::beginTransaction();

        try{

            $input['name'] = $input['first_name'].$input['last_name'];
            $input['password'] = str_random(7);

            $user = $this->model->create($input);

            $role_user = new RoleUser();
            $role_user->role_id = $input['role_id'];
            $role_user->user_id = $user->id;
            $role_user->save();

            \DB::commit();

            return $user;

        }catch (Exception $e){

            \DB::rollback();
            return false;
        }

    }

这是我的测试

/** @test */
    public function testUserRepoStoreFail()
    {
        $roleUser = factory(\App\Models\RoleUser::class)->create();
        $role = factory(\App\Models\Role::class)->create();

        $this->expectException(Exception::class);

        $data =[
            'role_id' => $role->id,
            'name' => $this->faker->name,
            'first_name' => $this->faker->name,
            'last_name' => $this->faker->name,
            'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
            'remember_token' => str_random(10),
        ];

        $rep = new UserRepositoryEloquent($this->container);
//
        $user = $rep->store($data);


    }

我删除了电子邮件以使商店功能失败

当我这样做时,我得到:

1) Tests\Unit\AuthTest::testUserRepoStoreFail 断言抛出“Exception”类型的异常失败。**

【问题讨论】:

  • 这基本上是说您期望使用$this->expectException(Exception::class) 会出现异常,但是当您使用store() 方法时,不会抛出异常。
  • 另外,如果你没有,在你的类的顶部说use Exception;,就在命名空间之后,或者在catch()和你的测试中使用\Exception而不是Exception断言。如果您不执行其中一项,您的异常命名空间将与使用它的类相同,并且两个异常将不匹配

标签: javascript php laravel phpunit


【解决方案1】:

即使抛出异常,它也会在store-方法中被捕获和处理。 expectException 期待未处理的异常。 如果您想在您的方法中测试 catch-block,您必须断言 DB::rollback() 已被调用,或者只是该方法返回 false 而不是用户实例。

【讨论】:

    【解决方案2】:

    测试没有通过可能是因为当您错过电子邮件时没有抛出异常。检查您在进行手动测试时是否遇到任何异常。如果不尝试像这样自己添加异常:

    public function store(array $input){
    
            \DB::beginTransaction();
    
            try{
    
                $input['name'] = $input['first_name'].$input['last_name'];
                $input['password'] = str_random(7);
    
                //Add this check
                if(empty($input['email'])){
                    throw new \InvalidArgumentException("Email is empty");
                }
    
                $user = $this->model->create($input);
    
                $role_user = new RoleUser();
                $role_user->role_id = $input['role_id'];
                $role_user->user_id = $user->id;
                $role_user->save();
    
                \DB::commit();
    
                return $user;
    
            }catch (Exception $e){
    
                \DB::rollback();
                return false;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 2015-04-29
      • 2015-10-17
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多