一直沒做過單元測試,對單元測試、功能測試的區別還是不理解
兩個問題:
我目前的項目中 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);}
回複內容:
一直沒做過單元測試,對單元測試、功能測試的區別還是不理解
兩個問題:
我目前的項目中 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);}
可以臨時修改Yii::$classMap重新導向AnotherService這個類到你的類比類中,或者重寫一個依賴注入的邏輯
我認為單元測試就是針對單元做測試,針對一系列做測試確實是功能測試
但怎樣才算一系列呢,如果說mysql_query這個函數算一個單元的話,那這個函數的底層不也是執行了一系列的步驟嗎?那這算不算一個功能?
所以如何界定單元和功能應該是要好好界定一下的,我的界定方法就是將接近PHP原生的對象方法理解為單元,沒有過多層次的調用封裝的
開發人員自己將業務串聯起來一起測試的這就是功能測試
你的單元測試代碼基本是沒錯的,只是當這個測試案例有多個測試方法時,before方法會被多次調用,解決方案請見
http://www.kkh86.com/it/codeception/guide-unit-test-before-after.html
我有個簡單的demo,還沒空完善,請見
http://www.kkh86.com/it/codeception/guide-unit-test-apply.html
內容下面有下載連結
你邀請了誰?