nuonomaduro/laravel-mojito

用于测试 Laravel 视图的轻量级包。

v0.2.10 2022-01-13 14:37 UTC

This package is auto-updated.

Last update: 2024-09-10 10:57:34 UTC


README

Mojito example

Build Status Total Downloads Latest Version License

关于 Mojito

Mojito 由 Nuno Maduro 创建并维护,是一个用于独立测试 Laravel 视图的轻量级包。

安装与使用

需要 PHP 8.0+

使用 Composer 安装 Mojito

composer require nunomaduro/laravel-mojito --dev

如何使用

class WelcomeTest extends TestCase
{
    // First, add the `InteractsWithViews` trait to your test case class.
    use InteractsWithViews; 

    public function testDisplaysLaravel()
    {
        // Then, get started with Mojito using the `assertView` method.
        $this->assertView('welcome')->contains('Laravel');
    }
}

可选地,您也可以从 HTTP 测试中执行视图测试

class WelcomeTest extends TestCase
{
    public function testDisplaysLaravel()
    {
        $response = $this->get('/');

        $response->assertStatus(200);

        $response->assertView()->contains('Laravel');
    }
}

contains

断言视图包含指定的文本。

$this->assertView('button')->contains('Click me');
$this->assertView('button', ['submitText' => 'Cancel'])->contains('Cancel');

$this->assertView('welcome')->in('title')->contains('Laravel');
$this->assertView('welcome')->in('.content')->contains('Nova');

empty

断言视图没有文本内容。

注意:在此检查中不考虑空 HTML 节点。

$this->assertView('empty')->in('.empty-div')->empty();

first

过滤视图并仅返回匹配选择器的第一个元素。

$this->assertView('welcome')->first('.links a')->contains('Docs');

has

断言视图具有指定的选择器。

$this->assertView('button')->has('button');

$this->assertView('welcome')->has('head');
$this->assertView('welcome')->in('body')->has('.content');

hasAttribute

断言视图的 根元素 具有指定的属性值。

$this->assertView('button')->hasAttribute('attribute', 'value');
$this->assertView('button')->hasAttribute('data-attribute', 'value');

$this->assertView('welcome')->hasAttribute('lang', 'en');
$this->assertView('welcome')->in('head')->first('meta')->hasAttribute('charset','utf-8');

hasClass

断言视图具有具有给定类的元素。

$this->assertView('button')->hasClass('btn');

$this->assertView('welcome')->in('.content')->at('div > p', 0)->hasClass('title');

hasLink

断言视图具有具有给定链接的元素。

$this->assertView('button')->hasLink(route('welcome'));

$this->assertView('welcome')->in('.links')->first('a')->hasLink('https://laravel.net.cn/docs');
$this->assertView('welcome')->in('.links')->at('a', 6)->hasLink('https://vapor.laravel.net.cn');
$this->assertView('welcome')->in('.links')->last('a')->hasLink('https://github.com/laravel/laravel');

in

过滤视图并仅返回匹配选择器的元素。

$this->assertView('welcome')->in('.links a')->contains('Laracast');

last

过滤视图并仅返回匹配选择器的最后一个元素。

$this->assertView('welcome')->last('.links a')->contains('GitHub');

hasMeta

断言视图在 head 部分具有指定的元标签。

$response->assertView()->hasMeta(['property' => 'og:title']);
$response->assertView()->hasMeta(['property' => 'og:title', 'content' => 'Laravel']);

可宏化

您可以为 ViewAssertion::class 添加自己的宏。

use NunoMaduro\LaravelMojito\ViewAssertion;

// Within a service provider:
ViewAssertion::macro('hasCharset', function (string $charset) {
    return $this->in('head')->first('meta')->hasAttribute('charset', $charset);
});

// In your tests:
$this->assertView('welcome')->hasCharset('utf-8');

贡献

感谢您考虑为 Mojito 贡献。所有贡献指南均在此处提及:CONTRIBUTING.md

您可以查看 CHANGELOG 以获取持续更新和有关更改的详细信息。您还可以关注推特账户以获取最新公告或只是打个招呼: @enunomaduro

支持开发

您喜欢这个项目吗?通过捐赠来支持它

许可

Mojito 是一个开源软件,许可协议为 MIT 许可协议