【问题标题】:View not found on my laravel package tests在我的 laravel 包测试中找不到视图
【发布时间】:2021-10-18 01:49:44
【问题描述】:

我正在尝试为我的 Laravel package 添加刀片指令。

一切都很好,但在测试阶段,我收到了这个错误:

1) MahbodHastam\UserWallet\Tests\Feature\BladeTest::show_user_wallet_balance
InvalidArgumentException: View [userWalletBalance] not found.

我应该把$this->loadViewsFrom(...)放在服务提供者中吗?

查看路径:/tests/resources/views/userWalletBalance.blade.php

测试路径:/tests/Feature/BladeTest.php

我还尝试将resources 目录移动到Feature 目录(在BladeTest.php 旁边),但得到了同样的错误。

这是我在BladeTest.php上的代码:

public function show_user_wallet_balance() {
    $wallet = UserWallet::getWallet(wallet_id: 1);

    $this->assertEquals('1000', $this->renderView('userWalletBalance', ['wallet' => $wallet]));
}

protected function renderView($view, $with) {
    Artisan::call('view:clear');

    return trim((string) view($view)->with($with));
}

【问题讨论】:

  • 您可以参考laravelpackage.com/09-routing.html#views了解更多关于在包中加载视图的信息。
  • 文档说要在整个包上加载视图,但我想在测试中加载视图以测试我之前添加的刀片指令。

标签: php laravel package package-development


【解决方案1】:

我修好了:D

我没有从文件中加载视图,而是使用InteractsWithViews trait(位于Illuminate\Foundation\Testing\Concerns 命名空间)来呈现刀片,如下面的代码:

$renderedView = (string) $this->blade("@userWalletBalance($wallet)");

$this->assertEquals('1000', trim($renderedView));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2013-02-10
    • 2020-01-20
    • 2012-05-15
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    相关资源
    最近更新 更多