Search code examples
rubyweb-servicessoapwsdlautomated-tests

Is it possible to generate a Ruby SOAP web service from a WSDL file?


My situation is as follows. Our test automation is implemented using cucumber. The website under test is written in .NET. This website interacts with the database via separate .NET WCF SOAP web services. The database must be populate with test data before each test is run.

I'd like to fake the repsonses of these SOAP web services in order to eliminate having to interact with the database.

Rather than create duplicate web services from scratch it would be great if I could create the web services from the WSDL files and then override any web service methods so they return the data I specify.

Is it possible to achieve this with existing gems?


Solution

  • I created my own mocking framework based on Savon, Sinatra and Mocha. It's available at https://github.com/kieranmaine/soap_mocker.

    The usage is as follows. Instantiate the mock service by passing in the WSDL url, specifying the service and port (within the WSDL) to use and also the path and port to run the mock service under:

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

    This will setup the mock service on the following URL:

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

    You then need to set up the mock responses:

    # 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"}})
    

    And finally start up the mock web service:

    service.run