【发布时间】:2018-05-25 00:56:53
【问题描述】:
我正在尝试模拟(仅作为示例)$user->posts()->get()。
示例服务:
use App\Models\User;
class SomeClass{
public function getActivePost(User $user): Collection
{
return $user->posts()->get();
}
}
和我的模型: 和型号:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use \App\Models\Post;
class User extends Model
{
public function posts() : HasMany
{
return $this->hasMany(Post::class);
}
}
这不起作用:
$this->user = Mockery::mock(User::class);
$this->user
->shouldReceive('wallets->get')
->andReturn('test output');
错误: TypeError:Mockery_2_App_Models_User::posts() 的返回值必须是 Illuminate\Database\Eloquent\Relations\HasMany 的实例,返回的 Mockery_4__demeter_posts 的实例
没有返回类型提示(在 post() 方法上)一切正常。我必须修改 andReturn() 吗?不知道怎么做
【问题讨论】:
-
该错误似乎与您的代码中的任何内容都不对应。
标签: laravel unit-testing eloquent phpunit