【问题标题】:PHPUnit and Jenkins: Datetime expected and Datetime actualPHPUnit 和 Jenkins:预期日期时间和实际日期时间
【发布时间】:2018-06-08 05:12:31
【问题描述】:

我有测试课:

class ClassTest extends \PHPUnit_Framework_TestCase
{
   /**
     * @covers \Path\Class::method()
     * @dataProvider methodData
     */
    public function testMethod($data, $expected)
    {
        $this->object = $this->getMockBuilder("Path\Class")
            ->setConstructorArgs([..])
            ->getMock();
        ....
        $response = $this->object->method($data);
        $this->assertEquals($expected, $response);
    }
    public function methodData()
    {
        $entity= new Entity();
        $entity->setDateArrivee(new \DateTime());
        ....
        ....
    }
}

我在 Jenkins 作业中运行 PHPunit。响应中 dateArrivee 的日期时间与预期的日期时间不同。我不知道为什么。 你能帮帮我吗?

【问题讨论】:

  • 这在很大程度上取决于您如何从 methodData() 数据提供者返回数据,以及在被调用的 method() 中发生了什么。 ClockMock-ing 或存储初始化日期时间以在函数中使用,并且 $expected 返回也会有所帮助。
  • 您是否在数据提供者/测试中多次创建DateTime 实例?如果是这样,它们之间可能存在细微差别((微)秒)。确保使用相同的时间戳生成日期,例如通过硬编码(当前您使用当前时间戳)。

标签: datetime jenkins phpunit


【解决方案1】:

我在testMethod中写了这个方法

$entity->setDateArrivee(new \DateTime());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多