Codeception 改进就绪单元测试

Codeception improve readiness unit test

我是测试新手,我正在使用 codeception 和 phpunit 做一些 TDD。

但是,我的方法有很多代码。我使用了最佳实践吗?有没有办法提高我的代码的就绪性,可以更干净吗?

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. 您可能可以将 testShouldHandleASuccessfulFormSubmissionForAddANews 和 testShouldASuccessfulFormSubmissionForEditANews 的正文提取到其他方法,参数如 $action = 'add'|'edit' (或使用您定义的常数 NewsFormHandler::EDIT等),因为它们几乎相同。

  2. 您可以将上述方法中的模拟创建提取到单个参数化方法中,因为过程几乎相同(将差异作为方法参数传递并让它完成肮脏的工作)。

  3. 您还可以使用 BDD 样式增加一些可读性,如页面 http://codeception.com/docs/05-UnitTests#BDD-Specification-Testing

  4. 中的示例