1

I have a project started with Laravel Jetstream, and when running a test on a Livewire component that uses $request->session(), I get an error message:

Illuminate\View\ViewException : Session store not set on request. (View: /var/www/html/vendor/livewire/livewire/src/views/mount-component.blade.php)

It's happening on two Livewire components that use the session, but for reference, here's the smaller component. Front end:

<div class="m-6">
    <x-range-slider :options="$options" wire:model="range.min,range.max" wire:click="onChange" />
</div>

And here's the back end:

class TierSlider extends Component
{
    public array $options = [
        "start" => [1, 4],
        "step" => 1,
        "range" => [
            "min" => [1],
            "max" => [4],
        ],
        "connect" => true,
        "behaviour" => "tap-drag",
        "pips" => [
            "mode" => "values",
            "stepped" => true,
            "density" => 100,
            "values" => [1, 2, 3, 4],
        ],
    ];
    public array $range;
    public int $armorId;

    public function render()
    {
        return view("livewire.tier-slider");
    }

    public function mount(Request $request)
    {
        $armorAndTiers = $request
            ->session()
            ->get("armors.{$this->armorId}", ["minTier" => 1, "maxTier" => 4]);
        $this->options["start"] = array_values($armorAndTiers);
        $this->range = [
            "min" => strval($armorAndTiers["minTier"]),
            "max" => strval($armorAndTiers["maxTier"]),
        ];
    }

    public function onChange()
    {
        $this->emit("updateShoppingList", [
            $this->armorId => [
                "minTier" => intval($this->range["min"]),
                "maxTier" => intval($this->range["max"]),
            ],
        ]);
    }
}

I could probably make this work by using the global helper session() rather than $request->session(), but it kinda seems like the Laravel docs don't encourage that. Also, the app works fine as is; it's only during testing that I get this error. Speaking of which, here's the beginning of my test class:

class TierSliderTest extends TestCase
{
    /** @test */
    public function the_component_can_render()
    {
        $component = Livewire::test(TierSlider::class, ["armorId" => 4]);

        $component->assertStatus(200);
    }
}

So what do I need to do to avoid this error during testing?

Sturm
  • 689
  • 2
  • 23
  • 52
  • Have you used web middleware? – Hefaz Oct 16 '22 at 05:41
  • @Hefaz How do you mean? – Sturm Oct 16 '22 at 16:04
  • You'll need to use the web middleware if you need session state, CSRF protection, and more. Route::group(['middleware' => ['web']], function () { // your routes here }); – Hefaz Oct 16 '22 at 17:12
  • Does this answer your question? [Laravel - Session store not set on request](https://stackoverflow.com/questions/34449770/laravel-session-store-not-set-on-request) – steven7mwesigwa Oct 16 '22 at 17:45
  • 1
    Those don't quite answer the question, I'm afraid. In [this screencast on Livewire lifecycle hooks](https://laravel-livewire.com/screencasts/hooks) at about the 1:19 mark, the presenter shows that you can dependency-inject in the `mount()` method of a Livewire component. That's great for getting `$request->session()`. But now I need to do this in my _Test Case_ of the component. I don't have a Request object, so how do I get access to one _within the test_? – Sturm Oct 16 '22 at 21:35

0 Answers0