【发布时间】:2018-03-03 10:11:49
【问题描述】:
Laravel 中的路由模型绑定,有助于将实例绑定到服务容器,以用于控制器操作。美妙的是,当我想编写测试时,我可以将一个模拟实例绑定到应用程序,它取代了绑定到容器的内容。 - App::instance($className, $mockInstance);
我已向路由 (Route::get("/{class_name}")) 提出请求。我想返回一个类的实例,这取决于路由参数class_name 的值是什么。
如果值为two,我想要Two 类的实例,One 类的实例也是如此。请注意,这两个类都继承自 AbstractClass,因此返回的实例类型为 AbstractClass,(PHP7)。
问题:当我访问路线时,我得到了正确的实例。但是当我想使用模拟实例时,它会覆盖模拟实例并创建/返回主实例。我需要使用模拟实例,以便对它设置期望。
我添加了一些代码,希望能说明我的意思。
abstract class AbstractClass {
// abstract parent class
}
class One extends AbstractClass {
// single child class
}
class Two extends AbstractClass {
// another single child class
}
// I put this inside the `bind` method of the RouteServiceProvider
Route::bind("class_name", function (string $class_name) : AbstractClass {
// I wish to return an instance to the route, based on the string that was passed - "one" or "two"
$class = ucfirst($class_name);
return new $class();
});
// test
public function testSomething () {
$mockInstance = Mockery::mock(AbstractClass::class);
App::instance(AbstractClass::class, $mockInstance);
$response = $this->get("/one");
}
// controller method
public function action (AbstractClass $instance) {
// I want to see/use the mock instance here, for testing
// and the main instance otherwise.
}
我用的是PHP7.0,用Laravel5.4开发
【问题讨论】:
-
我尝试过使用
$this->app->runningUnitTests(),首先在绑定之前检查我是否正在运行测试,这允许我使用模拟实例,但这也会影响我不想使用的测试模拟实例,但主要实例。在某些测试用例中,由于对某些外部 API 的某些调用,我需要模拟实例。
标签: php laravel testing mocking mockery