我尝试为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次。
我在做什么错了?
答案 0 :(得分:0)
您正在模拟整个类,因此不会运行scrape方法中的代码。您必须进行部分模拟,以保留任何未明确模拟的方法。
编辑:
您必须从容器中解决它,然后对此进行调用:
app(Scrape::class)->scrape('www.google.com');