代码之家  ›  专栏  ›  技术社区  ›  Rafael Redrado

在Codeception TestCase的before函数中找不到类

  •  0
  • Rafael Redrado  · 技术社区  · 11 年前

    我正在用Yii2和Codeception编写一些测试,在 _before() 功能:

    FATAL ERROR. TESTS NOT FINISHED.
    Class 'app\lib\FTPImage' not found in /var/www/proyect/tests/codeception/unit/lib/FTPImageTest.php:13
    

    这很奇怪,因为如果我在测试方法中使用它,它就像魅力一样:/

    以下是测试代码:

    <?php
    namespace tests\codeception\unit\lib;
    
    use Yii;
    use yii\codeception\TestCase;
    use app\lib\FTPImage;
    
    class FTPImageTest extends TestCase{
    
        public $ftp;
    
        public function _before(){
            $this->ftp = new FTPImage();
        }
    
        public function _after(){
        }
    
        public function testGetImagesNoImages(){
            $ftp = new FTPImage();
    
            $images = $ftp->getImages("123", "Foobar");
    
            $this->assertEmpty($images);
        }
    }
    

    这是FTPImage类的代码:

    <?php
    namespace app\lib;
    
    use Yii;
    use app\lib\FTPClient;
    use \yii\base\Exception;
    use \yii\base\ErrorException;
    use \yii\imagine\Image;
    
    class FTPImage{     
    
        public function __construct() {
        }
    
        public function getImages($reference, $supplier_name){
            ...
        }
    

    我希望有人能帮我
    提前感谢!

    1 回复  |  直到 11 年前
        1
  •  2
  •   Rafael Redrado    11 年前

    最后我找到了解决办法。我已更改为 _before _after 函数到 setUp tearDown 。这样,我可以使用我的FTPImage类。

    以下是我所做的:

    class FTPImageTest extends TestCase{
    
        public $ftp;
    
        public function setUp(){
            parent::setUp();
            $this->ftp = new FTPImage();
        }
    
        public function tearDown(){
            parent::tearDown();
        }
    
        ...
    }