1

以下のテストの1つをコメントアウトすると、テストに合格します。ただし、両方を一緒に実行すると、最後の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

テストを実行した後に $view->Dispose() を呼び出してみましたか? 私はvfsStreamの専門家ではありませんが、ファイルはテスト間で開いたままになるようです.現在のファイル位置が読み取り後に巻き戻されない場合、ファイルの最後に残り、2回目以降のテストで失敗します. .

于 2012-12-17T23:18:31.043 に答える
0

これが発生するのは、通常、適切にテストしているメソッドを分離していないためです。言い換えれば、PHPUnit は、テストしている (または類似した) 特定のメソッドの別のインスタンスを意味していることを知りません。したがって、2 番目のテストは常に失敗します。

同じメソッドを複数回使用する場合は、コード内で実行される適切な回数で「at」宣言を使用する必要があります。このようにして、PHPUnit はどちらを意味するかを認識し、期待/アサーションを適切に満たすことができます。

以下は、メソッド「run」が数回使用される一般的な例です。

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 に答える