在单元测试中嘲笑时未找到Laravel外观类

时间:2013-06-25 09:53:52

标签: phpunit laravel laravel-4 mockery

我可能在这里遗漏了一些东西,但我有一个非常简单的助手类来创建一个目录:

// Helper class

<?php namespace MyApp\Helpers;

    use User;
    use File;

    class FileSystemHelper
    {
        protected $userBin = 'users/uploads';

        public function createUserUploadBin(User $user)
        {
            $path = $this->userBin . '/' . $user->id;

            if ( ! File::isDirectory($path))
            {
                File::makeDirectory($path);
            }
        }
    }

此处有相关测试:

// Associated test class

<?php 

    use MyApp\Helpers\FileSystemHelper;

    class FileSystemHelperTest extends TestCase {

        protected $fileSystemHelper;

        public function setUp()
        {
            $this->fileSystemHelper = new FileSystemHelper;
        }

        public function testNewUploadBinCreatedWhenNotExists()
        {
            $user = new User; // this would be mocked

            File::shouldReceive('makeDirectory')->once();

            $this->fileSystemHelper->createUserUploadBin($user);
        }
    }

但是在运行测试时出现致命错误:

  

PHP致命错误:/my/app/folder/app/tests/lib/myapp/helpers/FileSystemHelperTest.php中找不到类'文件'

我看过嘲弄门面的文档,我看不出我出错的地方。有什么建议吗?

由于

2 个答案:

答案 0 :(得分:22)

我在文档中错过了这个:

  

注意:如果您定义自己的setUp方法,请务必调用parent :: setUp。

呼吁解决问题。卫生署!

答案 1 :(得分:4)

这是因为laravel框架在使用Facade之前没有加载或它因为你没有使用laravel php单元(TestCase类) 这是一个示例代码,用于测试app中的案例/ //注意从TestCase扩展而不是()

/**
 * TEST CASE the application.
 *
 */
class TestCase extends Illuminate\Foundation\Testing\TestCase {

    /**
     * Creates the application.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;

        $testEnvironment = 'testing';

        //this line boot the laravel framework so all facades are in your hand
        return require __DIR__.'/../../bootstrap/start.php';
   }

}

/** * TEST CASE the application. * */ class TestCase extends Illuminate\Foundation\Testing\TestCase { /** * Creates the application. * * @return \Symfony\Component\HttpKernel\HttpKernelInterface */ public function createApplication() { $unitTesting = true; $testEnvironment = 'testing'; //this line boot the laravel framework so all facades are in your hand return require __DIR__.'/../../bootstrap/start.php'; } }