如何解决Laravel 单元测试软件
我是单元测试新手,我想尝试一种方法,如果可能,强制删除模型,否则如果该模型有任何关系,则使用软删除。
代码运行良好,但如果我调用 ModelDeleteManager,softdelete 测试总是失败。 如果我在控制器中直接调用 $company->delete() ,则测试通过。
强制删除测试总是通过 ModelDeleteManager
销毁方法:
public function destroy ( ModelDeleteManager $model_delete_manager,Company $company) : JsonResponse
{
$model_delete_manager->processDelete ($company);
return \Response ::json ( [ 'success' => true ] );
}
模型删除管理器:
namespace App\Services;
use Bugsnag\BugsnagLaravel\Facades\Bugsnag;
use Exception;
use Illuminate\Database\Eloquent\Model;
class ModelDeleteManager
{
public function processDelete ( Model $model )
{
try {
if ( method_exists ( $model,'forceDelete' ) ) {
$model -> forceDelete ();
} else {
$model -> delete ();
}
} catch ( Exception $e ) {
if ( $e -> getCode () == "23000" ) {
$model -> delete ();
} else {
Bugsnag ::notifyException ( $e );
}
}
}
}
我的公司测试:
namespace Tests\Feature;
use App\Models\Company;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class CompanyTest extends TestCase
{
use RefreshDatabase,withFaker;
private $company;
private $user;
public function setUp () : void
{
parent ::setUp ();
$this -> company = Company ::factory () -> create ();
$this -> user = User ::factory () -> create ( [ 'company_id' => $this -> company -> id ] );
}
public function test_an_admin_can_forcedelete_a_company_without_relations ()
{
$new_company = Company ::factory () -> create ();
$this -> assertDatabaseHas ( 'companies',[ 'name' => $new_company -> name,'id' => $new_company -> id ] );
$response = $this -> actingAs ( $this -> user ) -> delete ( '/settings/company/' . $new_company -> id );
self ::assertEquals ( 200,$response -> getStatusCode () );
$this -> assertDatabaseMissing ( 'companies','id' => $new_company -> id ] );
}
public function test_an_admin_can_softdelete_a_company_with_relations ()
{
$this -> withoutExceptionHandling ();
$this -> assertDatabaseHas ( 'companies',[ 'name' => $this -> company -> name,'id' => $this -> company -> id ] );
$response = $this -> actingAs ( $this -> user ) -> delete ( '/settings/company/' . $this -> company -> id );
self ::assertEquals ( 200,$response -> getStatusCode () );
$this -> assertSoftDeleted ( 'companies',['id' => $this -> company -> id,'name' => $this -> company -> name ] );
}
}
这是我在测试中收到的失败错误,
Failed asserting that any soft deleted row in the table [companies] matches the attributes {"id":1,"name":"Ferre De Greef"}.
Found: [
{
"id": "1","name": "Ferre De Greef","street": "boulevard Kaya","city": "Tielt","post_code": "8995","TVA": "BE 0804617624","language": "da","bank": "BE02872154362405","swift": "YCFUWO29","phone": "+7648326849037","email": "kaat81@demeyer.org","website": "http:\/\/www.smets.com\/dolore-SAEpe-culpa-quae","longitude": null,"latitude": null,"created_at": "2021-01-06 08:55:36","updated_at": "2021-01-06 08:55:36","deleted_at": null
}
].
但只有测试失败,代码完美运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。