PHPUnit模拟使用mock

时间:2015-02-04 16:09:09

标签: php dependency-injection mocking phpunit unit-testing

我有两个类,我想通过PHPUnit测试它。 但是我在嘲笑这些东西时做错了。我想改变第一类调用的方法。

class One {
    private $someVar = null;
    private $abc = null;

    public function Start() {
        if ( null == $this->someVar) {
            $abc = (bool)$this->Helper();
        }

        return $abc;
    }

    public function Helper() {
        return new Two();
    }


}
Class Two {
    public function Check($whateverwhynot) {
        return 1;
    }
}


Class testOne extends PHPUnit_Framework_TestCase {
    public function testStart() {
        $mockedService = $this
            ->getMockBuilder(
                'Two',
                array('Check')
            )
            ->getMock();

        $mockedService
            ->expects($this->once())
            ->method('Check')
            ->with('13')
            ->will($this->returnValue(true));

        $mock = $this
            ->getMockBuilder(
                'One',
                array('Helper'))
            ->disableOriginalConstructor()
            ->getMock();

        $mock
            ->expects($this->once()) 
            ->method('Helper')
            ->will($this->returnValue($mockedService));

        $result = $mock->Start();
        $this->assertFalse($result);
    }
}

结果是$resultNULL,而不是'true'

如果我不使用断言行,我会收到一条错误消息:

F

Time: 0 seconds, Memory: 13.00Mb

There was 1 failure:

1) testOne::testStart
Expectation failed for method name is equal to <string:Check> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

想法?

更新   - 环境:PHP 5.4.3x,PHPUnit 3.7.19   - 一件重要的事情:无法修改原始类(Class One和Class Two)

2 个答案:

答案 0 :(得分:2)

您可以使用ouzo-goodies中的模拟。

类:

class One
{
    private $someVar = null;
    private $abc = null;
    private $two;

    public function __construct(Two $two)
    {
        $this->two = $two;
    }

    public function Start()
    {
        if (null == $this->someVar) {
            $abc = (bool)$this->Helper()->Check(1213);
        }

        return $abc;
    }

    public function Helper()
    {
        return $this->two;
    }
}

在构造函数中注入对象Two,因此您可以轻松地模拟此对象。

class Two
{
    public function Check($whateverwhynot)
    {
        return 1;
    }
}

测试:

class OneTest extends PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function shouldCheckStart()
    {
        //given
        $mock = Mock::create('\Two');
        Mock::when($mock)->Check(Mock::any())->thenReturn(true);

        $one = new One($mock);

        //when
        $start = $one->Start();

        //then
        $this->assertTrue($start);
    }
}

mocks的文档。

答案 1 :(得分:0)

你正在创建你的模拟错误。您正在使用$this->getMockBuilder(),它只接受一个参数,即要模拟的类的名称。你好像把它与$this->getMock()混为一谈。

将测试更改为:

public function testStart() {
    $mockedService = $this
        ->getMockBuilder('Two')
        ->setMethods(array('Check'))
        ->getMock();

    $mockedService
        ->expects($this->once())
        ->method('Check')
        ->with('13')
        ->will($this->returnValue(true));

    $mock = $this
        ->getMockBuilder('One')
        ->setMethods(array('Helper'))
        ->disableOriginalConstructor()
        ->getMock();

    $mock
        ->expects($this->once()) 
        ->method('Helper')
        ->will($this->returnValue($mockedService));

    $result = $mock->Start();
    $this->assertFalse($result);
}

这不是一个好的测试,因为我们需要模拟我们正在测试的类,但是你说你根本不能改变类。