php - Laravel Mocking Repository Controller - 来自 Mockery\Interface 的方法“all”应该被准确地调用 1 次,但被调用 0 次
问题描述
我在控制器内模拟一个存储库,但是当我调用CompanyController@index
(反过来调用CompanyRepository@all
)时,测试返回这个错误:
Test\Unit\Shared\Repositories\CompanyRepositoryTest::test_all Mockery\Exception\InvalidCountException: 来自 Mockery_0__App_Shared_Repostiries_CompanyRepository 的方法 all() 应准确调用 1 次,但调用 0 次。
<?php
namespace Test\Unit\Shared\Repositories;
use Mockery;
use Tests\TestCase;
class CompanyRepositoryTest extends TestCase
{
protected $companyRepo;
protected $company;
public function setUp(): void
{
parent::setUp();
}
public function tearDown(): void
{
parent::tearDown();
}
/**
* @group mockingrepo3
*/
public function test_all()
{
$this->companyRepo = Mockery::mock('\App\Shared\Repositories\CompanyRepository');
$this->companyRepo->shouldReceive('all')
->andReturn(new \Illuminate\Support\Collection)
->once();
$this->company = \App\Models\Company::find(3);
$this->be($this->company);
$this->app->instance('\App\Shared\Repositories\CompanyRepository', $this->companyRepo);
$response = $this->call('GET', route('app.admin.company.index'));
// $this->assertEquals(new \Illuminate\Support\Collection, $companies);
}
}
公司控制器
<?php
class CompanyController extends Controller
{
private $companyRepo;
public function __construct(CompanyRepository $companyRepo)
{
$this->companyRepo = $companyRepo;
}
public function index(Request $request)
{
$companies = $this->companyRepo->all();
return view("admin.company.index")->with(['companies' => $companies]);
}
}
解决方案
推荐阅读
- node.js - 使用 React Web 应用程序的 SSO
- java - 矩形不更新也不显示颜色
- naming - 命令中布尔参数的命名
- yii2 - Yii2:为gridview添加数据选择器
- amazon-redshift - 为什么不使用带有交错排序键的时间戳?
- android - 用于检查 SPEN 是否附加在手机中的 API?
- gitlab - 使用共享运行器的 GitLab CI/CD 配置问题
- android - 转包在一个 Java 对象中出错警告(使用 safeUnbox 方法)
- javascript - jQuery Keypad / Primefaces 键盘重新映射空格键
- c# - 如何在 C# 中使用 Directory.GetFiles() 添加多个文件?