How to unit test PHP traits

后端 未结 2 1584
别跟我提以往
别跟我提以往 2021-01-30 20:36

I want to know if there is a solution on how to unit-test a PHP trait.

I know we can test a class which is using the trait, but I was wondering if there are better appro

2条回答
  •  梦如初夏
    2021-01-30 21:09

    You can also use getObjectForTrait , then assert the actual result if you want.

    class YourTraitTest extends TestCase
    {
        public function testGetQueueConfigFactoryWillCreateConfig()
        {
            $obj = $this->getObjectForTrait(YourTrait::class);
    
            $config = $obj->getQueueConfigFactory();
    
            $this->assertInstanceOf(QueueConfigFactory::class, $config);
        }
    
        public function testGetQueueServiceWithoutInstanceWillCreateConfig()
        {
            $obj = $this->getObjectForTrait(YourTrait::class);
    
            $service = $obj->getQueueService();
    
            $this->assertInstanceOf(QueueService::class, $service);
        }
    }
    

提交回复
热议问题