使用PHPUnit,我想知道如何从同一个存根/模拟中获得多个期望.
例如,我想测试mock将display()调用该方法并返回NULL.我还想测试一下这个方法process()是否会被调用.
事实上,我的测试被称为testProcessIsCalledIfDisplayReturnNull().
所以我需要在同一个模拟对象上设置2个期望值,并且手册并没有真正帮助:(
如何重置 PHPUnit Mock 的 expects()?
我有一个 SoapClient 的模拟,我想在测试中多次调用它,重置每次运行的期望。
$soapClientMock = $this->getMock('SoapClient', array('__soapCall'), array($this->config['wsdl']));
$this->Soap->client = $soapClientMock;
// call via query
$this->Soap->client->expects($this->once())
->method('__soapCall')
->with('someString', null, null)
->will($this->returnValue(true));
$result = $this->Soap->query('someString');
$this->assertFalse(!$result, 'Raw query returned false');
$source = ConnectionManager::create('test_soap', $this->config);
$model = ClassRegistry::init('ServiceModelTest');
// No parameters
$source->client = $soapClientMock;
$source->client->expects($this->once())
->method('__soapCall')
->with('someString', null, null)
->will($this->returnValue(true));
$result = $model->someString();
$this->assertFalse(!$result, 'someString returned false');
Run Code Online (Sandbox Code Playgroud)