本文介绍了使用PHPUnit测试RESTful Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以让我知道如何使用PHPUnit测试RESTful Web服务吗? PHPUnit似乎没有该功能.

Can anyone please let me know how to test the RESTful web services using PHPUnit? PHPUnit doesn't seem to have that capability.

推荐答案

将请求抽象到请求对象.这样,您就可以测试您的代码,而不必实际发出真正的请求.然后进行测试就很容易.

Abstract the Request into a Request Object. This way you can test your code without actually having to make real Requests. Testing that is easy then.

class RequestTest extends PHPUnit_Framework_TestCase
{
    public function testRequest()
    {
        $request = new Request();
        $request->setMethod('PUT');
        $request->setPutData(…);
        $this->assertSomething(
            $this->testSubjectUsingRequest->process($request)
        );
    }
}

如果要从Web服务测试 响应 ,请对Web服务的API进行模拟/存根.

In case you want to test the responses from a Webservice, mock/stub the API of the Webservice.

PHPUnit章节中有一章关于 Web服务的存根和模拟,尽管建议的内置Web服务模拟功能适用于带有WSDL的Soap Services,所以您必须手动配置Mocks(就像您对Mocks进行配置一样).任何模拟资源).

There is a chapter in the PHPUnit chapter about Stubbing and Mocking Web Services although the suggested in-built webservice mocking facilities apply to Soap Services with WSDL, so you'd have to configure your Mocks by hand (just as you would with any mocked resource).

如果这不能回答您的问题,请使用有关RESTful服务的更多详细信息来更新您的问题.

If this doesn't answer your question please update your question with more details about the RESTful service what you are trying to do/test with it.

这篇关于使用PHPUnit测试RESTful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:45