【问题标题】:Codeception improve readiness unit testCodeception 改进准备单元测试
【发布时间】:2017-03-21 20:22:13
【问题描述】:

我是测试新手,我正在使用 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);

    }



}

【问题讨论】:

    标签: phpunit codeception


    【解决方案1】:
    1. 您可能可以将 testShouldHandleASuccessfulFormSubmissionForAddANews 和 testShouldHandleASuccessfulFormSubmissionForEditANews 的主体提取到其他带有 $action = 'add'|'edit' 等参数的方法(或使用您定义的内容 NewsFormHandler::EDIT 等),因为它们几乎是一样。

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 2015-03-18
      • 2013-08-29
      • 2014-01-27
      • 1970-01-01
      相关资源
      最近更新 更多