代码之家  ›  专栏  ›  技术社区  ›  tom

PHPUnit如何模拟新创建的模型

  •  0
  • tom  · 技术社区  · 14 年前

    我是如何为下面的代码编写测试的。我想模拟$userModel,但如何将其添加到测试中?

    class PC_Validate_UserEmailDoesNotExist extends Zend_Validate_Abstract
    {
        public function isValid($email, $context = NULL)
        {
            $userModel = new Application_Model_User();
            $user = $userModel->findByEmailReseller($email, $context['reseller']);
    
            if ($user == NULL) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    }
    

    我确实将我的类更改为以下内容以使其可测试,它现在使用依赖注入。有关依赖注入的更多信息,请参阅 here

    我现在这样称呼这个班:

    new PC_Validate_UserEmailDoesNotExist(new Application_Model_User()
    

    重构类

    class PC_Validate_UserEmailDoesNotExist extends Zend_Validate_Abstract
    {
        protected $_userModel;
    
        public function  __construct($model)
        {
            $this->_userModel = $model;
        }
    
        public function isValid($email, $context = NULL)
        {
            if ($this->_userModel->findByEmailReseller($email, $context['reseller']) == NULL) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    }
    

    单元测试

    class PC_Validate_UserEmailDoesNotExistTest extends BaseControllerTestCase
    {
        protected $_userModelMock;
    
        public function setUp()
        {
            parent::setUp();
            $this->_userModelMock = $this->getMock('Application_Model_User', array('findByEmailReseller'));
        }
    
        public function testIsValid()
        {
            $this->_userModelMock->expects($this->once())
                            ->method('findByEmailReseller')
                            ->will($this->returnValue(NULL));
    
            $validate = new PC_Validate_UserEmailDoesNotExist($this->_userModelMock);
            $this->assertTrue(
                    $validate->isValid('jef@test.com', NULL),
                    'The email shouldn\'t exist'
            );
        }
    
        public function testIsNotValid()
        {
            $userStub = new \Entities\User();
    
            $this->_userModelMock->expects($this->once())
                            ->method('findByEmailReseller')
                            ->will($this->returnValue($userStub));
    
            $validate = new PC_Validate_UserEmailDoesNotExist($this->_userModelMock);
            $this->assertFalse(
                    $validate->isValid('jef@test.com', NULL),
                    'The email should exist'
            );
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Gordon Haim Evgi    10 年前

    有三种解决方法:

    1) 将使用的类名设置为可配置的,这样您就可以执行以下操作:

    $className = $this->userModelClassName;
    $userModel = new $className();
    

    public function isValid($email, $context = NULL, $userModel = NULL)
    {
        if($userModel === NULL)
        {
            $userModel = new Application_Model_User();
        }
        // ...
    }
    

    或3)使用 set_new_overload() 如中所述

    注: the Test-Helper extension is superseded https://github.com/krakjoe/uopz