1

我的情况如下。我们的测试自动化是使用黄瓜实现的。被测网站是用 .NET 编写的。该网站通过单独的 .NET WCF SOAP Web 服务与数据库交互。在运行每个测试之前,必须使用测试数据填充数据库。

我想伪造这些 SOAP Web 服务的响应,以便不必与数据库交互。

如果我可以从 WSDL 文件创建 Web 服务然后覆盖任何 Web 服务方法以便它们返回我指定的数据,那么与其从头开始创建重复的 Web 服务,那就太好了。

是否有可能用现有的宝石实现这一目标?

4

1 回答 1

3

我基于 Savon、Sinatra 和 Mocha 创建了自己的模拟框架。它可在https://github.com/kieranmaine/soap_mocker获得。

用法如下。通过传入 WSDL url 来实例化模拟服务,指定要使用的服务和端口(在 WSDL 中)以及在以下位置运行模拟服务的路径和端口:

service = SoapMocker::MockServiceContainer.new 
    "http://www.webservicex.net/uklocation.asmx?WSDL", 
    "UKLocation", 
    "UKLocationSoap", 
    "/mock/UkLocationSoapService", 
    {:port => 1066}

这将在以下 URL 上设置模拟服务:

http://localhost:1066/mock/UKLocationSoapService.

然后,您需要设置模拟响应:

# Set up responses for valid requests
service.mock_operation "GetUKLocationByPostCode",
  {:GetUKLocationByPostCode => {:PostCode => "SW1A 0AA"}},
  {:GetUKLocationByPostCodeResponse => {:GetUKLocationByPostCodeResult =>     "House Of Commons, London, SW1A 0AA, United Kingdom"}}

# Example of accessing mock object directly.
service.io_mock.stubs(:call_op)
  .with("GetUKLocationByPostCode", regexp_matches(/AL1 4JW/))
  .returns({:GetUKLocationByPostCodeResponse => {:GetUKLocationByPostCodeResult => "TESTING"}})

最后启动模拟网络服务:

service.run
于 2013-10-28T09:22:53.450 回答