dongyi2159 2018-12-13 15:24
浏览 152

在Laravel 5.7中测试PHP单元测试之间的依赖关系

I am working on Laravel 5.7 and i'd like to ask you a question regarding the PHPUnit testing.

I have a test class, let's say ProductControllerTest.php, with two methods testProductSoftDelete() and testProductPermanentlyDelete(). I want to use the annotation @depends in the testProductPermanentlyDelete() in order to soft-delete first a product and then get the product id and proceed to permanently deletion test. The problem here is that the DatabaseTransaction trait runs the transactions in every test (method) execution. I need to start the transaction before all the tests of my ProductControllerTest class and then rollback the transaction at the end of all tests. Do you have any ideas? From what i have searched from the web nothing worked properly.

public function testProductSoftDelete()
{
   some code

   return $product_id;
}

/**
* @depends testProductSoftDelete
*/
public function testProductPermanentlyDelete($product_id)
{
   code to test permanently deletion of the product with id $product_id.
   There is a business logic behind that needs to soft delete first a 
   product before you permanently delete it.

}

Does the following make sense?

    namespace Tests\App\Controllers\Product;

    use Tests\DatabaseTestCase;
    use Tests\TestRequestsTrait;

    /**
     * @group Coverage
     * @group App.Controllers
     * @group App.Controllers.Product
     *
     * Class ProductControllerTest
     *
     * @package Tests\App\Controllers\Product
     */

    class ProductControllerTest extends DatabaseTestCase
    {
        use TestRequestsTrait;

        public function testSoftDelete()
        {
            $response = $this->doProductSoftDelete('9171448');
            $response
                ->assertStatus(200)
                ->assertSeeText('Product sof-deleted successfully');
        }


        public function testUnlink()
        {
            $this->doProductSoftDelete('9171448');
            $response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/unlink/9171448');

            $response
                ->assertStatus(200)
                ->assertSeeText('Product unlinked successfully');
        }
    }


    namespace Tests;

    trait TestRequestsTrait
    {
        /**
         * Returns the response
         *
         * @param $product_id
         * @return \Illuminate\Foundation\Testing\TestResponse
         */
        protected function doProductSoftDelete($product_id)
        {
            $response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/soft-delete/'.$product_id);
            return $response;
        }

    }

    namespace Tests;

    use Illuminate\Foundation\Testing\DatabaseTransactions;

    abstract class DatabaseTestCase extends TestCase
    {

        use CreatesApplication;
        use DatabaseTransactions;
    }
  • 写回答

1条回答 默认 最新

  • doushi7394 2018-12-13 15:26
    关注

    Create a separate function to execute the same behavior twice:

    public function testProductSoftDelete()
    {
        doSoftDelete();
    }
    
    public function testProductPermanentlyDelete()
    {
        doSoftDelete();
        doPermanentDelete();
    }
    

    Your case isn't a case of test dependency, but what you really want to do is to check if a soft deleted can be permanent deleted (or something like that). Creating a dependency, in this case, will increase complexity of the test. It's usually better to mount the test scenario from scratch (data/objects), then execute the logic, and then test if the actual scenario is the expected.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀