PHPackages                             noeldemartin/laravel-dusk-mocking - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. noeldemartin/laravel-dusk-mocking

Abandoned → [pestphp/pest](/?search=pestphp%2Fpest)ArchivedLibrary[Testing &amp; Quality](/categories/testing)

noeldemartin/laravel-dusk-mocking
=================================

Mock facades in Laravel Dusk tests.

v6.5.3(5mo ago)36107.4k↓17.3%5[3 issues](https://github.com/NoelDeMartin/laravel-dusk-mocking/issues)MITPHPPHP &gt;=7.2.0CI failing

Since Apr 7Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/NoelDeMartin/laravel-dusk-mocking)[ Packagist](https://packagist.org/packages/noeldemartin/laravel-dusk-mocking)[ RSS](/packages/noeldemartin-laravel-dusk-mocking/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (0)

Laravel Dusk Mocking [![Build Status](https://camo.githubusercontent.com/4496b48511b27c98fab60aaab4e6811f4b555182de4286a3326ecb5d6ab3fd18/68747470733a2f2f7472617669732d63692e6f72672f4e6f656c44654d617274696e2f6c61726176656c2d6475736b2d6d6f636b696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/NoelDeMartin/laravel-dusk-mocking) [![Github Actions Status](https://github.com/noeldemartin/laravel-dusk-mocking/workflows/Testing/badge.svg)](https://github.com/noeldemartin/laravel-dusk-mocking/actions)
==================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#laravel-dusk-mocking--)

Warning

This package is not necessary anymore, given that [Pest added support for browser tests with mocking in v4](https://pestphp.com/docs/pest-v4-is-here-now-with-browser-testing#content-pest-v4-is-here-now-with-browser-testing).

Still, if you haven't migrated to Pest yet (or you don't want to), you should be able to keep using this package, but keep in mind that it's not in active development. Feel free to [contact me](https://noeldemartin.com) if you *really* need some help. You can also use [vendor-patches](https://github.com/symplify/vendor-patches) to apply any changes that you need.

When running browser tests with Laravel Dusk, [it is not possible](https://github.com/laravel/dusk/issues/152) to mock facades like [it is usually done](https://laravel.com/docs/mocking) for http tests. This package aims to provide that functionality. However, it does so by doing some workarounds. It is recommended to read the [Disclaimer](#disclaimer) (and in particular the [Limitations](#limitations)) before using it.

Before adding it to your project, you can also give it a try with a bare-bones Laravel application prepared with tests running on a CI environment here: [laravel-dusk-mocking-sandbox](https://github.com/NoelDeMartin/laravel-dusk-mocking-sandbox/).

Installation
============

[](#installation)

Install using composer:

```
composer require --dev noeldemartin/laravel-dusk-mocking

```

Add the following code to your base test case (usually `DuskTestCase`).

```
use NoelDeMartin\LaravelDusk\Browser;

...

protected function newBrowser($driver)
{
    return new Browser($driver);
}
```

Usage
=====

[](#usage)

The conceptual usage is the same as can be learned on Laravel's documentation about [mocking](https://laravel.com/docs/5.6/mocking). The only difference is that in Dusk, mocking can be set up independently on each browser instance. For that reason, instead of calling static methods we will call instance methods. Look at the following example on how to mock the Mail facade:

```
public function testOrderShipping()
{
    $this->browse(function ($browser) use ($user) {
        $mail = $browser->fake(Mail::class);

        $browser->visit('...')
                // Perform order shipping...
                ->assertSee('Order purchased! Check your email for details!');

        $mail->assertSent(OrderShipped::class, function ($mail) use ($order) {
            return $mail->order->id === $order->id;
        });

        // Assert a message was sent to the given users...
        $mail->assertSent(OrderShipped::class, function ($mail) use ($user) {
            return $mail->hasTo($user->email) &&
                   $mail->hasCc('...') &&
                   $mail->hasBcc('...');
        });

        // Assert a mailable was sent twice...
        $mail->assertSent(OrderShipped::class, 2);

        // Assert a mailable was not sent...
        $mail->assertNotSent(AnotherMailable::class);
    });
}
```

Notice how the api is the same as Http tests.

Configuration
-------------

[](#configuration)

Drivers serialize mocking data through requests, and cookies are used by default. The drawback is that using cookies, there is a size limit for how much information can be stored (4KB). For that reason, different drivers can be configured. Create a file named `dusk-mocking.php` inside your application config folder to change the default driver:

```
