Summer Sale! All accounts are 50% off this week.

bencarter78@hotmail.com's avatar

Mockery fetchMock on null error

Hello

I'm just starting out with Mockery and have read/watched some information on getting started but I am running up against this error

Call to a member function fetchMock() on null

I don't understand why it's null, I thought it just creates an object to create expectations on. I get the same error when I try to mock any class. Here's my test

<?php

use Mockery\Mock as m;
use Maatwebsite\Excel\Excel;

class ExcelWriterTest extends TestCase
{
    /**
     * @test
     */
    function it_creates_an_excel_document()
    {
        $excel = m::mock(Excel::class);
    }

    function tearDown()
    {
        m::close();
    }

}

What am I doing wrong?

0 likes
5 replies
dirkolbrich's avatar

Try setting it to:

$excel = m::mock('Maatwebsite\Excel\Excel');
$this->app->instance('Maatwebsite\Excel\Excel', $excel);
dirkolbrich's avatar

What is in your ExcelWriter class. What do you want to test?

bencarter78@hotmail.com's avatar

@dirkolbrich It's a helper class that creates a spreadsheet but I've just created a dummy class to see if I can get it working and I get the same error.

my test class is now

<?php

use Mockery\Mock as m;

class ExcelWriterTest extends TestCase
{
    /**
     * @test
     */
    function it_creates_an_excel_document()
    {
        $excel = m::mock('App\Services\Dummy');
    }
}

// App/Services/Dummy
<?php

namespace App\Services;

class Dummy {
    public function testing()
    {
        return 'This is just a test.';
    }
}

It still errors with

Call to a member function fetchMock() on null

Is it something I'm not doing?

bencarter78@hotmail.com's avatar
Level 14

I found what my mistake was, for some reason I thought I needed to...

use Mockery\Mock as m;

I should have just put

use Mockery as m;
3 likes

Please or to participate in this conversation.