php - 如何让PHPUnit等待条件测试?

标签 php if-statement phpunit tdd file-exists

每次运行测试时,它都会在第一次运行时失败,在第二次运行时通过。

这是代码:

 /** @test */
    public function some_function_test()
        {
            $file = file_exists($this->path.'file');

            if ($file) {
                echo "\n file exists! \n";
            }else{
                $this->createFile;
            }

         $this->assertEquals($file, true);

        }

当我删除文件并再次运行测试时,它失败了。 这告诉我断言在我的 if 语句之前运行。

如果断言首先运行,我可以让它等待我的条件测试吗?

最佳答案

您的断言将永远if之前运行。

您的测试失败,因为在 else 分支中,在使用 createFile 创建文件后,您没有更改 $file,因此在 else 分支 $file 仍然是 false。我想您需要将 $file 更改为 true:

public function some_function_test()
{
    $file = file_exists($this->path.'file');

    if ($file) {
        echo "\n file exists! \n";
    }else{
        $this->createFile();    // you're calling a method, aren't you?
        $file = true;
    }

    $this->assertEquals($file, true);
    // or simplier:
    // $this->assertTrue($file);
}

关于php - 如何让PHPUnit等待条件测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377355/

相关文章:

php - 无法访问 Controller 响应的正确内容

php - Woocommerce 在管理订单详细信息上显示自定义字段数据

php - 数组到字符串错误

C# 'if' 绑定(bind)值

php - 在 PHP 中根据日期隐藏链接

java - 池塘嵌套循环中的简单java鱼

phpunit - 在 PHPunit 的命令行上设置 xdebug.coverage_enable=On

laravel - 如何测试失败后一定时间后发布的工作?

php - 如何在没有原始 fla 文件的情况下制作 swf 链接?

php - 预填写表格(无法控制表格代码)