【发布时间】:2020-01-16 04:23:05
【问题描述】:
我尝试为我的 Laravel 6.0 应用程序运行 phpunit 测试,但在尝试模拟方法时,我收到此消息,因为它找不到类/方法:
Mockery\Exception\InvalidCountException:来自 Mockery_2_App_Http_Controllers_Scraper 的方法 scrapeGoogleData() 应该是 恰好调用了 1 次,但调用了 0 次。
我的测试代码是:
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');
}
“scrapeGoogleData”方法肯定会运行,因为它是从 Scraper::scrape 方法调用的。但是出于某种原因,嘲弄看不到这一点。我收到此错误:
Mockery\Exception\InvalidCountException:来自 Mockery_2_App_Http_Controllers_Scraper 的方法 scrapeGoogleData() 应该是 恰好调用了 1 次,但调用了 0 次。
我做错了什么?
【问题讨论】: