首页 > 解决方案 > 在我的 laravel 包测试中找不到视图

问题描述

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

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

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));
}

标签: phplaravelpackagepackage-development

解决方案


我修好了:D

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

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

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

推荐阅读