【发布时间】:2015-03-10 13:50:07
【问题描述】:
下面的例子可以很好地说明我的问题:
class a
{
function a()
{
return file_get_contents('http://some/third/party/service');
}
}
class b
{
function b()
{
$a = new a();
return $a->a() . ' Bar';
}
}
class testB extends test
{
function testB()
{
$b = new b();
// Here we need to override a::a() method in some way to always return 'Foo'
// so it doesn't depend on the third party service. We only need to check
// the $b::b() method's behavior (that it appends ' Bar' to the string).
// How do we do that?
$this->assert_equals('Foo Bar', $b->b());
}
}
让我指出,我无法控制“a”类的定义/包含位置。
【问题讨论】:
-
查看反射 api。
-
@MarcelBurkhard 似乎没有提供必要的功能。
-
这是 DI 或工厂可以帮助您在测试中消除此问题的地方:
function b(a $a { return $a->a() . ' Bar'; }使得模拟a的实例变得更加容易