英文:
Laravel assertViewIs with view from a package
问题
Sure, here's the translation of the code-related content:
我有多个组件构成的 CMS,建立在 Laravel 10 之上。在这个示例中,有一个 UI 组件和一个核心组件。
在我的 core
组件中,我定义了这个路由,它返回来自我的 UI 组件的视图:
Route::get('php-test', function () {
return view('my-cms::dashboard.home');
});
我想要测试这个路由,在我的核心组件的 composer.json
中,我添加了 UI 组件:
"require-dev": {
"my-cms/ui": "dev-master"
},
然后,在我的核心组件中编写了一个包含以下测试方法的测试:
/** @test */
public function test_route_returns_view() : void
{
$this->withoutExceptionHandling();
$response = $this->get('php-test');
$response->assertOk();
$response->assertViewIs('my-cms::dashboard.home');
}
这会返回以下错误:
InvalidArgumentException: No hint path defined for [my-cms].
在 UI 组件的 ServiceProvider 中,我注册了视图:
public function boot() : void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'my-cms');
}
当视图来自另一个组件时,我如何测试路由是否返回视图?
If you have any more code-related questions or need further assistance, feel free to ask.
英文:
I have multiple packages that make up a CMS built on top of Laravel 10. In this example: a UI package and a core package.
In my core
I define this route, which returns a view from my UI package:
Route::get('php-test', function () {
return view('my-cms::dashboard.home');
});
I want to test this route, so in the composer.json
of my core package I add the UI package:
"require-dev": {
"my-cms/ui": "dev-master"
},
Then I write a test in my core package which contains this test method:
/** @test */
public function test_route_returns_view() : void
{
$this->withoutExceptionHandling();
$response = $this->get('php-test');
$response->assertOk();
$response->assertViewIs('my-cms::dashboard.home');
}
This returns the following error:
> InvalidArgumentException: No hint path defined for [my-cms].
In the ServiceProvider of the UI package I register the views:
public function boot() : void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'my-cms');
}
How can I test if a route returns a view, when that view is from another package?
答案1
得分: 0
这个错误是因为我猜测当包没有安装在实际应用程序中时,ServiceProvider 没有运行。不确定这是否是最佳解决方案,但它有效。
在使用其他包的测试中,我注册了其他包的 ServiceProvider:
protected function getPackageProviders($app) : array
{
return array_merge(
parent::getPackageProviders($app),
[\MyCms\UI\MyCmsUIServiceProvider::class]
);
}
这样做可以使视图被注册,错误就消失了。
英文:
This is error is thrown because I guess the ServiceProvider isn't ran when the package is not installed in an actual application. Not sure if this is the best solution but it works.
In the tests that use other packages I register the ServiceProvider of the other package:
protected function getPackageProviders($app) : array
{
return array_merge(
parent::getPackageProviders($app),
[\MyCms\UI\MyCmsUIServiceProvider::class]
);
}
This makes it so that the views are getting registered and the error dissapears.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论