yii2 - 关于PHP的单元测试,好难~~
天蓬老师
天蓬老师 2017-04-10 17:42:59
0
2
463

一直没做过单元测试,对单元测试、功能测试的区别还是不理解

两个问题:

  • 有没有开源的程序,带单元测试的例子,推荐个。最好是codecept单元测试框架的程序。 希望通过阅读代码来加深对单元测试的理解。

  • 说说我目前对单元测试的认识,欢迎指正。

我目前的项目中 controller->service->repository->entity
目前我只对service层做单元测试

class ProductServiceTest extends \Codeception\TestCase\Test
{
    use Specify;

    protected $tester;
    private $_service;

    protected function _before()
    {
        $this->_service = Yii::createObject(ProductService::className());

        //mock repository
        $this->_service->repository = Stub::make(ProductRepository::className(), [
            'save' => function () {
                return new ProductEntity();
            },
        ]);
    }

    protected function _after()
    {
    }

    public function testSave()
    {
        $this->specify("保存产品", function () {  
            $data = [];         
            $this->assertInstanceOf(
                ProductEntity::className(),
                $this->_service->save($data));
        });
    }

上面是我写的最基本的例子,测试ProductService类的测试类

我测试了保存商品的方法,在_before时,为ProductRepository打桩,ProductRepository::save()直接返回ProductEntity实例。

感觉怪怪的,这样的单元测试,只是测试这个方法能不能跑起来,对里面的逻辑因为打桩的关系,都略过了。

如果要测试具体的逻辑,比如能不能保存到数据库中,就需要entity做测试,需要对数据库做操作,那这样,还是单元测试么?我感觉变成的功能测试了。

另外,能通过注入repository属性的方法打桩,是因为ProductService中用了依赖注入的方法,如果
ProductService::save()方法中不是用了依赖注入ProductRepository,而是内部的一个service,就不能通过属性注入的方式打桩,这种情况要怎么处理?

ProductService类
public function save($data){
    if(empty($data)){
        //这里的AnotherService如何打桩?
        $service = Yii::createObject(AnotherService::className());
        $data = $service->getData();
    }
    
  return $this->repository->save($data);
}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!