【发布时间】:2019-01-12 00:23:24
【问题描述】:
我有这样的情况。我有一些第 3 方特征(我不想测试)并且我有使用此特征的特征,并且在某些情况下运行第 3 方特征方法(在下面的示例中,我总是运行它)。
当我有这样的代码时:
use Mockery;
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
/** @test */
public function it_runs_parent_method_alternative()
{
$class = Mockery::mock(B::class)->makePartial();
$class->shouldReceive('fooX')->once();
$this->assertSame('bar', $class->foo());
}
protected function tearDown()
{
Mockery::close();
}
}
trait X {
function foo() {
$this->something->complex3rdpartyStuff();
}
}
trait Y2 {
function foo() {
$this->fooX();
return 'bar';
}
}
class B {
use Y2, X {
Y2::foo insteadof X;
X::foo as fooX;
}
}
它会正常工作,但我不希望代码像这样组织。在上面的类代码中,我使用了这两个特征,但在代码中我想测试实际上 trait 使用了开头提到的其他特征。
但是当我有这样的代码时:
<?php
use Mockery;
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
/** @test */
public function it_runs_parent_method()
{
$class = Mockery::mock(A::class)->makePartial();
$class->shouldReceive('fooX')->once();
$this->assertSame('bar', $class->foo());
}
protected function tearDown()
{
Mockery::close();
}
}
trait X {
function foo() {
$this->something->complex3rdpartyStuff();
}
}
trait Y {
use X {
foo as fooX;
}
function foo() {
$this->fooX();
return 'bar';
}
}
class A {
use Y;
}
我明白了:
未定义的属性 $something
所以在这种情况下,Mockery 似乎不再模拟 X::foo 方法了。有没有办法用这样组织的代码编写这样的测试?
【问题讨论】:
标签: php unit-testing testing phpunit mockery