0
votes

I try to run a phpunit test for my Laravel 6.0 application, but when trying to mock a method, I get this message as it cannot find the class/method:

Mockery\Exception\InvalidCountException: Method scrapeGoogleData() from Mockery_2_App_Http_Controllers_Scraper should be called exactly 1 times but called 0 times.

My test code is:

namespace Tests\Feature;

use Tests\TestCase;
use App\Http\Controllers\Scraper;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ScrapeTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function test_scrapeGoogleData() {
        $this->mock(Scraper::class, function ($mock) {
            $mock->shouldReceive('scrapeGoogleData')->once();
        });
        Scraper::scrape('www.google.com');
    }

The "scrapeGoogleData" method is definitely ran as it's called from the Scraper::scrape method. But for some reason, mockery cannot see that. I get this error:

Mockery\Exception\InvalidCountException: Method scrapeGoogleData() from Mockery_2_App_Http_Controllers_Scraper should be called exactly 1 times but called 0 times.

What am I doing wrong?

1

1 Answers

0
votes

You're mocking the whole class, so the code in your scrape method is not run. You'll have to make a partial mock, to keep any method which is not explicitly mocked.

EDIT:

You'll have to resolve it from the container and then call scrape on that:

app(Scrape::class)->scrape('www.google.com');