如何在laravel中使用辅助函数对视图进行单元测试

时间:2016-08-30 06:56:56

标签: php laravel unit-testing laravel-4 helper

我有这些测试用例访问网址并检查正确的行为,但我无法使其工作,因为它在视图中的帮助函数上出错。

它适用于浏览器,但不适用于测试。

    /** @test */
    public function setUp()
    {
        parent::setUp();

    }

    /** @test **/   
    public function openAdminDashboardUnauthorized()
    {    
        $crawler = $this->client->request('GET', 'admin/organizations');
        $this->assertTrue($this->client->getResponse()->isOk());

    }

    /** @test **/
    public function tearDown()
    {
        parent::tearDown();            
    }

继承错误

enter image description here

修改 我的帮助器类加载在classLoader的global.php中 顺便说一句,这是在laravel 4.2

更新

人们说你必须模拟辅助函数才能对它们进行单元测试。但在我的情况下,辅助函数在视图中,我测试了从setup函数转储静态消息,实际上它在测试运行时没有加载。

2 个答案:

答案 0 :(得分:0)

所以这就是我发现的工作。

在composer.json自动加载子树

中加载了我的助手类

并快速运行“composer dump-autoload”

"autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ],
        "files": [
            "app/helper/Helper.php"
        ]
    }

现在我知道在global.php中加载类和通过composer自动加载之间的区别。如果有人能解释更多关于laravel启动应用程序的顺序,我会接受。

答案 1 :(得分:0)

使用Mockery,laravel4.2默认使用它。见https://laravel.com/docs/4.2/testing 你应该在顶部有这样的东西。

viewHelperMock

我们将自己的Error: spawn EACCES at exports._errnoException (util.js:890:11) at ChildProcess.spawn (internal/child_process.js:302:11) at exports.spawn (child_process.js:379:9) at /home/titan5/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:322:13 at Promise._execute (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/debuggability.js:284:9) at Promise._resolveFromExecutor (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:480:18) at new Promise (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:77:14) at PHPCSFixer.module.exports.Beautifier.spawn (/home/titan5/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:319:16) at /home/titan5/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:281:10 at tryCatcher (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:509:31) at Promise._settlePromise (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:566:18) at Promise._settlePromise0 (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:611:10) at Promise._settlePromises (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:690:18) at Promise._fulfill (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:635:18) at PromiseArray._resolve (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise_array.js:125:19) at PromiseArray._promiseFulfilled (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise_array.js:143:14) at Promise._settlePromise (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:571:26) at Promise._settlePromise0 (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:611:10) at Promise._settlePromises (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:690:18) at Async._drainQueue (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:138:16) at Async._drainQueues (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:148:10) at Async.drainQueues (/home/titan5/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:17:14) 实例绑定到Laravel的DI容器。因此,每当对ViewHelper类发出请求时,它都会解析为我们的mock实例。因此,现在您可以设置视图助手中调用的所有方法的期望值。

如果您向我们展示您的视图(html)代码,那么指导您将更容易。不要感到沮丧,一开始就很棘手,令人困惑。因此,请多次尝试同时查看Laravel's IOC container的工作原理。

如果您有任何问题,请告诉我们。