1

如果我注释掉下面的测试之一,则测试通过。但是,同时运行两者,最后一个将失败(即使我更改测试顺序):

生产代码:

<?php
class View
{
    private $filename;
    private $data;

    public function __construct($filename)
    {
        $this->filename = $filename;
        $this->data = [];
    }

    public function __set($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function render()
    {
        extract($this->data);
        ob_start();
        require_once $this->filename;
        return ob_get_clean();
    }

    public function __toString()
    {
        return $this->render();
    }
}

测试类:

require_once 'vendor/autoload.php';

use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
/**
 * @outputBuffering enabled
 */
class ViewTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        vfsStream::setup();
    }

    /**
     * @outputBuffering enabled
     */
    public function testRenderSimpleView()
    {
        $fileContent = 'index file';
        vfsStreamWrapper::getRoot()->addChild(
            vfsStream::newFile('index.php')->withContent($fileContent)
        );
        $view = new View(vfsStream::url('index.php'));
        echo $view->render();
        $this->expectOutputString($fileContent);
    }

    /**
     * @outputBuffering enabled
     */
    public function testRenderViewWithData()
    {
        $filename = 'index.php';
        $fileContent = '<?php echo $a; ?>';
        vfsStreamWrapper::getRoot()->addChild(
            vfsStream::newFile($filename)->withContent($fileContent)
        );
        $view = new View(vfsStream::url($filename));
        $view->a = 1;
        echo $view;
        $this->expectOutputString('1');
    }
}

测试输出:

PHPUnit 3.7.10 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 3.75Mb

There was 1 failure:

1) ViewTest::testRenderViewWithData
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'1'
+''


FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

这对我来说没有任何意义。我错过了什么?

4

2 回答 2

0

When that happens, it's usually because you're not isolating the method that you are testing properly. In other words, PHPUnit doesn't know you mean a different instance of a certain method that you are testing (or similar). Therefore, the second test always fails.

In cases where you use the same method more than once, you should use the "at" declaration with the proper count where executed in the code. This way PHPUnit knows which one you mean, and can fulfill the expectation/assertion properly.

The following is a generic example where method 'run' is used several times:

public function testRunUsingAt()
    {
        $test = $this->getMock('Dummy');

        $test->expects($this->at(0))
            ->method('run')
            ->with('f', 'o', 'o')
            ->will($this->returnValue('first'));

        $test->expects($this->at(1))
            ->method('run')
            ->with('b', 'a', 'r')
            ->will($this->returnValue('second'));

        $test->expects($this->at(2))
            ->method('run')
            ->with('l', 'o', 'l')
            ->will($this->returnValue('third'));

        $this->assertEquals($test->run('f', 'o', 'o'), 'first');
        $this->assertEquals($test->run('b', 'a', 'r'), 'second');
        $this->assertEquals($test->run('l', 'o', 'l'), 'third');
    }
于 2012-12-21T03:23:30.957 回答
0

您是否尝试在执行测试后调用 $view->Dispose() ?我不是 vfsStream 专家,但看起来文件在测试之间会保持打开状态,如果读取后当前文件位置没有倒带,它将保留在文件末尾,导致第二次和后续测试失败.

于 2012-12-17T23:18:31.043 回答