gpt4 book ai didi

php - 在 phpunit 中连接约束

转载 作者:可可西里 更新时间:2023-11-01 12:51:52 25 4
gpt4 key购买 nike

我的问题是,我如何将子句中的约束与 phpunit 连接起来?在虚拟示例中:

$test->expects ($this->once())
->method ('increaseValue')
->with ($this->greaterThan (0)
->will ($this->returnValue (null));

方法increaseValue的参数必须大于0,但如果我需要评估这个参数必须小于10。

我如何连接 $this->lessThan(10)

最佳答案

您可以使用logicalAnd 表达式:

$test->expects ($this->once())
->method ('increaseValue')
->with ($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
->will ($this->returnValue (null));

有关可能功能的列表,请查看以下功能:PHPUnit/Framework/Assert.php不以“assert”开头的

完整示例

<?php

class mockMe {
public function increaseValue($x) {
}
}


class fooTest extends PHPUnit_Framework_TestCase {

public function testMock() {
$test = $this->getMock('mockMe');
$test->expects($this->once())
->method('increaseValue')
->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
->will($this->returnValue(null));
$test->increaseValue(6);
}

public function testMockFails() {
$test = $this->getMock('mockMe');
$test->expects($this->once())
->method('increaseValue')
->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
->will($this->returnValue(null));
$test->increaseValue(12);
}

}

结果

 phpunit blub.php
PHPUnit 3.5.13 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 4.25Mb

There was 1 failure:

1) fooTest::testMockFails
Expectation failed for method name is equal to <string:increaseValue> when invoked 1 time(s)
Parameter 0 for invocation mockMe::increaseValue(<integer:12>) does not match expected value.
Failed asserting that <integer:12> is less than <integer:10>.

/home/.../blub.php:26

关于php - 在 phpunit 中连接约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5990914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com