如何在多个测试中模拟测试PHPUnit中的Web服务? [英] How to mock test a web service in PHPUnit across multiple tests?

查看:78
本文介绍了如何在多个测试中模拟测试PHPUnit中的Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHPUnit测试Web服务接口类.基本上,此类调用 SoapClient 对象.我正在尝试使用此处描述的getMockFromWsdl方法在PHPUnit中测试此类:

I am attempting to test a web service interface class using PHPUnit. Basically, this class makes calls to a SoapClient object. I am attempting to test this class in PHPUnit using the getMockFromWsdl method described here:

http://www.phpunit.de/manual/current/zh-CN/test-doubles.html#test-doubles.stubbing-and-mocking-web-services

但是,由于我想测试同一类中的多个方法,因此每次设置对象时,我还必须设置模拟WSDL SoapClient 对象.这导致引发致命错误:

However, since I want to test multiple methods from this same class, every time I setup the object, I also have to setup the mock WSDL SoapClient object. This is causing a fatal error to be thrown:

Fatal error: Cannot redeclare class xxxx in C:\web\php5\PEAR\PHPUnit\Framework\TestCase.php(1227) : eval()'d code on line 15

如何在多个测试中使用相同的模拟对象,而不必每次都从WSDL重新生成它?这似乎是问题所在.

How can I use the same mock object across multiple tests without having to regenerate it off the WSDL each time? That seems to be the problem.

-

被要求发布一些代码来查看,这是TestCase中的设置方法:

Having been asked to post some code to look at, here's the setup method in the TestCase:

protected function setUp() {
    parent::setUp();

    $this->client = new Client();

    $this->SoapClient = $this->getMockFromWsdl(
        'service.wsdl'
    );

    $this->client->setClient($this->SoapClient);
}

推荐答案

这不是理想的解决方案,但是在您的设置中,为SOAP模拟提供一个随机"类名称,例如

This isn't an ideal solution, but in your setup give the SOAP mock a "random" class name, for example

$this->_soapClient = $this->getMockFromWsdl( 'some.wsdl', 'SoapClient' . md5( time().rand() ) );

这样可以确保至少在调用安装程序时不会出现该错误.

This ensures that at least when the setup is called you don't get that error.

这篇关于如何在多个测试中模拟测试PHPUnit中的Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆