2016-11-08 22 views
0

我是新來測試,我使用codeception和phpunit來做一些TDD。Codeception提高準備就緒單元測試

但是,我的方法有很多代碼。我是否使用最佳做法?有沒有一種方法可以提高我的代碼的可用性,是否可以使更加乾淨

class NewsFormHandlerTest extends \Codeception\Test\Unit 
{ 
    /** 
    * @var \UnitTester 
    */ 
    protected $tester; 

    protected function _before() 
    { 
    } 

    protected function _after() 
    { 
    } 

    private function getFormMock(){ 

     return $this->getMockBuilder(FormInterface::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
    } 

    private function getNewsManagerMock(){ 

     return $this->getMockBuilder(INewsManager::class) 
      ->disableOriginalConstructor() 
      ->getMock(); 
    } 

    // tests 

    public function testShouldHandleASuccessfulFormSubmissionForAddANews() 
    { 

     // prepare 
     $request = new \Symfony\Component\HttpFoundation\Request(); 
     $news = new News(); 

     $form = $this->getFormMock(); 
     $form->expects($this->once()) 
      ->method('isValid') 
      ->will($this->returnValue(true)); 

     $form->expects($this->once()) 
      ->method('submit'); 

     $form->expects($this->once()) 
      ->method('getData') 
      ->will($this->returnValue($news)); 

     $newsManager = $this->getNewsManagerMock(); 
     $newsManager->expects($this->once()) 
      ->method('add'); 

     $user = Stub::make(WebserviceUser::class, []); 

     // test 
     $handler = new NewsFormHandler($newsManager, $user); 
     $newsReturned = $handler->handle($form, $request, NewsFormHandler::ADD); 

     // assert 
     $this->assertInstanceOf(News::class, $newsReturned); 
     $this->assertEquals($news, $newsReturned); 

    } 

    public function testShouldHandleASuccessfulFormSubmissionForEditANews() 
    { 

     // prepare 
     $request = new \Symfony\Component\HttpFoundation\Request(); 
     $news = new News(); 

     $form = $this->getFormMock(); 
     $form->expects($this->once()) 
      ->method('isValid') 
      ->will($this->returnValue(true)); 

     $form->expects($this->once()) 
      ->method('submit'); 

     $form->expects($this->once()) 
      ->method('getData') 
      ->will($this->returnValue($news)); 

     $newsManager = $this->getNewsManagerMock(); 
     $newsManager->expects($this->once()) 
      ->method('edit'); 

     $user = Stub::make(WebserviceUser::class, []); 

     // test 
     $handler = new NewsFormHandler($newsManager, $user); 
     $newsReturned = $handler->handle($form, $request, NewsFormHandler::EDIT); 

     // assert 
     $this->assertInstanceOf(News::class, $newsReturned); 
     $this->assertEquals($news, $newsReturned); 

    } 

    public function testFailFormWithInvalidData() 
    { 

     // prepare 
     $request = new \Symfony\Component\HttpFoundation\Request(); 

     $form = $this->getFormMock(); 
     $form->expects($this->once()) 
      ->method('isValid') 
      ->will($this->returnValue(false)); 


     $newsManager = $this->getNewsManagerMock(); 
     $newsManager->expects($this->never()) 
      ->method('edit'); 

     $this->expectException(InvalidFormException::class); 

     $user = Stub::make(WebserviceUser::class, []); 

     // test 
     $handler = new NewsFormHandler($newsManager, $user); 
     $newsReturned = $handler->handle($form, $request, NewsFormHandler::ADD); 

     // assert 
     $this->assertNull($newsReturned); 

    } 



} 

回答

1
  1. 你或許可以提取testShouldHandleASuccessfulFormSubmissionForAddANews的身體和testShouldHandleASuccessfulFormSubmissionForEditANews以其他方式與像$ action參數=「添加」 |「編輯」(或使用您定義的常數NewsFormHandler ::編輯等),因爲它們幾乎一樣。因爲過程幾乎相同(將差異作爲方法參數傳遞並讓它執行骯髒的工作),您可以從上述方法中提取模擬創建到單個參數化方法。

  2. 您可以通過使用BDD風格的頁面也添加一些可讀性,如示例http://codeception.com/docs/05-UnitTests#BDD-Specification-Testing