但是,我正在尝试测试创建追随者,因为我正在调用 Auth::user()->id 我收到错误“尝试获取非对象的属性”。目前我一直在构建一个输入数组,当我运行 create 方法时,验证是从 BaseModel 中的引导方法触发的。有没有办法我应该模拟 $input 数组,或者我应该在我的存储库的 create 方法中进行验证?
<?php
class FollowersTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->mock = $this->mock('Convoconnect\Storage\Follower\FollowerRepository');
}
public function mock($class)
{
$mock = Mockery::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
public function tearDown()
{
Mockery::close();
}
/**
* Test Store success
*/
public function testStoreSuccess()
{
$input = [
'user_id' => 1,
'follower_id' => 4,
];
$this->mock
->shouldReceive('create')
->once();
$this->call('POST', 'followers', $input);
$this->assertRedirectedToRoute('followers.index');
}
}
<?php
use Convoconnect\Storage\Follower\FollowerRepository as Follower;
class FollowersController extends BaseController {
/**
* Follower Repository
*
* @var Follower
*/
protected $follower;
public function __construct(Follower $follower) {
$this->follower = $follower;
}
public function store() {
$input = [
'user_id' => Auth::user()->id,
'follower_id' => Input::get('follower_id'),
];
$follower = $this->follower->create($input);
if($follower->save()) return Redirect::route('followers.index');
return Redirect::back()
->withInput()
->withErrors($follower->errors);
}
}