我想测试一个函数是否被调用了一组参数,例如toHaveBeenCalledWith
如何在jasmine中工作。
有没有办法在php单元测试中做到这一点?
答案 0 :(得分:1)
Phpunit没有茉莉所谓的间谍。但是你可以模拟一个类,并设置你期望如何调用该类的方法(以及它们应该返回的内容等)的期望。
结帐phpunit manual example 9.11.
public function testObserversAreUpdated()
{
// Create a mock for the Observer class,
// only mock the update() method.
$observer = $this->getMockBuilder(Observer::class)
->setMethods(['update'])
->getMock();
// Set up the expectation for the update() method
// to be called only once and with the string 'something'
// as its parameter.
$observer->expects($this->once())
->method('update')
->with($this->equalTo('something'));
// Create a Subject object and attach the mocked
// Observer object to it.
$subject = new Subject('My subject');
$subject->attach($observer);
// Call the doSomething() method on the $subject object
// which we expect to call the mocked Observer object's
// update() method with the string 'something'.
$subject->doSomething();
}