我的班级中有一个非常基本的功能,正如评论所说的那样,它只是将调用转发给子模型,如果它们在这个类中不存在。这在我的测试中非常有效。
/**
* Handles calling methods on the user model directly from the provider
* Allows e.g. Guardian::User()->findOrFail(1) without having to redeclare
* the methods.
*
* @param $method
* @param $parameters
*
* @return mixed
*/
public function __call($method, $parameters){
$user = $this->createModel();
return call_user_func_array([$user, $method], $parameters);
}
但是我也想为此编写单元测试,在这种情况下我尝试编写的测试:
public function testProviderAsksModelToFind(){
$factoryUser = Factory::attributesFor('User', ['id' => 1]);
$p = m::mock('Webfox\Guardian\User\Guardian\Provider[createModel]',['']);
$user = m::mock('Webfox\Guardian\User\Guardian\User[find]');
$p->shouldReceive('createModel')->once()->andReturn($user);
$user->shouldReceive('find')->with(1)->once()->andReturn($factoryUser);
$this->assertSame($factoryUser, $p->find(1));
}
然而,这吐出了下面可爱的错误:
1) EloquentUserProviderTest::testProviderAsksModelToFind BadMethodCallException: Method Webfox\Guardian\User\Guardian\Provider::find() 在这个模拟对象上不存在
那么,我该如何解决这个问题,以便我的测试通过?