【问题标题】:Mockery/Etsy PHPExtensions does not fail test if required methods are not called如果未调用所需的方法,则 Mockery/Etsy PHPExtensions 不会失败测试
【发布时间】:2013-02-24 07:17:26
【问题描述】:

我有下面的代码,我希望在运行时会失败,因为 DoesNothing 类不使用模拟类或在其上调用任何必需的方法。

<?php
class DoesNothing
{

}

class DoesNothingTest extends YourMockeryTestCase
{
    /**
     * @test
     */
    public function somethingIsCalled()
    {
        $this->mock = Mockery::mock();

        $keys = array(
            '1234',
            'abcxyz',
            '*&(%&^$-*/~@:{}',
            ')*&GA^FAUIB(*',
            '',
            ' ',
        );

        foreach ($keys as $key) {
            $this->mock
                ->shouldReceive('remove')
                ->atLeast()->times(1)
                ->with($key);
        }
        $var = new DoesNothing($this->mock);
    }
}

但是当我运行它时,它通过了。我希望它会说“未调用方法删除”等。

可能出了什么问题?与 Mockery 如何与 PHPUnit 对话有关?

谢谢, 马丁

编辑: 我还应该提到我们正在使用Etsy's PHPExtensions 将其集成到 PHPUnit 中

【问题讨论】:

    标签: php unit-testing tdd phpunit mockery


    【解决方案1】:

    你的方法名应该以test开头,否则PHPUnit不会判断为测试。

    public function testSomethingIsCalled()
    

    编辑

    您必须在您的拆卸方法中调用Mockery::close() 才能执行预期。即

    /**
     * Tear down
     */
    public function tearDown()
    {
        \Mockery::close();
    }
    

    【讨论】:

    • phpDoc 语法让 PHPUnit 知道这是一个没有前缀文档的测试:phpunit.de/manual/current/en/appendixes.annotations.html(滚动到底部)
    • @Martin Lyne,不知道。你每天学习新的东西 ;)。尝试在您的测试中添加一个断言,以 100% 确保您的测试至少正确执行。
    • :) 是的,我刚刚添加了$this-&gt;assertTrue(false),不幸的是,它解决了失败。 :(
    • 我们赢了!我认为 Etsy 的东西消除了对它的需求,但我错了。最烦人的部分是我一开始就在那里并删除了它。真是个蠢才!谢谢布拉姆。
    • docs.mockery.io/en/latest/reference/phpunit_integration.html 要集成 Mockery,您只需为包含 Mockery::close() 的测试定义一个 tearDown() 方法
    猜你喜欢
    • 2013-08-26
    • 2020-08-27
    • 2014-03-11
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多