作者:布瓜Pourqu2502854853 | 来源:互联网 | 2023-01-31 09:48
Given the following class:
鉴于以下课程:
Other = $Other;
}
public function query ()
{
$params = array(
'key1' => 'Value 1'
, 'key2' => 'Value 2'
);
$this->Other->post($params);
}
}
And this testcase:
而这个测试用例:
getMock('Other', array('post'));
$Mock->expects($this->once())
->method('post')
->with(YOUR_IDEA_HERE);
$Example = new Example($Mock);
$Example->query();
}
How do I verify that $params
(which is an array) and is passed to $Other->post()
contains a key named 'key1' that has a value of 'Value 1'?
如何验证$ params(这是一个数组)并传递给$ Other-> post()包含一个名为'key1'的键,其值为'Value 1'?
I do not want to verify all of the array - this is just a sample code, in actual code the passed array has a lot more values, I want to verify just a single key/value pair in there.
我不想验证所有的数组 - 这只是一个示例代码,在实际代码中传递的数组有更多的值,我想在那里只验证一个键/值对。
There is $this->arrayHasKey('keyname')
that I can use to verify that the key exists.
我可以使用$ this-> arrayHasKey('keyname')来验证密钥是否存在。
There is also $this->contains('Value 1')
, which can be used to verify that the array has this value.
还有$ this-> contains('Value 1'),可用于验证数组是否具有此值。
I could even combine those two with $this->logicalAnd
. But this of course does not give the desired result.
我甚至可以将这两个与$ this-> logicalAnd结合起来。但这当然不会产生预期的结果。
So far I have been using returnCallback, capturing the whole $params and then doing asserts on that, but is there perhaps another way to do what I want?
到目前为止,我一直在使用returnCallback,捕获整个$ params,然后对此进行断言,但是有没有其他方法可以做我想要的?
5 个解决方案