Giter Club home page Giter Club logo

savon_spec's Introduction

Savon::Spec Build Status

Savon testing library.

Installation

Savon::Spec is available through Rubygems and can be installed via:

$ gem install savon_spec

Expects

Include the Savon::Spec::Macros module into your specs:

RSpec.configure do |config|
  config.include Savon::Spec::Macros
end

By including the module you get a savon method to mock SOAP requests. Here's a very simple example:

let(:client) do
  Savon::Client.new do
    wsdl.endpoint = "http://example.com"
    wsdl.namespace = "http://users.example.com"
  end
end

before do
  savon.expects(:get_user)
end

it "mocks a SOAP request" do
  client.request(:get_user)
end

This sets up an expectation for Savon to call the :get_user action and the specs should pass without errors. Savon::Spec does not execute a POST request to your service, but uses Savon hooks to return a fake response:

{ :code => 200, :headers => {}, :body => "" }

To further isolate your specs, I'd suggest setting up FakeWeb to disallow any HTTP requests.

With

Mocking SOAP requests is fine, but what you really need to do is verify whether you're sending the right parameters to your service.

before do
  savon.expects(:get_user).with(:id => 1)
end

it "mocks a SOAP request" do
  client.request(:get_user) do
    soap.body = { :id => 1 }
  end
end

This checks whether Savon uses the SOAP body Hash you expected and raises a Savon::Spec::ExpectationError if it doesn't.

Failure/Error: client.request :get_user, :body => { :name => "Dr. Who" }
Savon::Spec::ExpectationError:
  expected { :id => 1 } to be sent, got: { :name => "Dr. Who" }

You can also pass a block to the #with method and receive the Savon::SOAP::Request before the POST request is executed.
Here's an example of a custom expectation:

savon.expects(:get_user).with do |request|
  request.soap.body.should include(:id)
end

Returns

Instead of the default fake response, you can return a custom HTTP response by passing a Hash to the #returns method.
If you leave out any of these values, Savon::Spec will add the default values for you.

savon.expects(:get_user).returns(:code => 500, :headers => {}, :body => "save the unicorns")

Savon::Spec also works with SOAP response fixtures (simple XML files) and a conventional folder structure:

~ spec
  ~ fixtures
    ~ get_user
      - single_user.xml
      - multiple_users.xml
  + models
  + controllers
  + helpers
  + views

When used inside a Rails 3 application, Savon::Spec uses Rails.root.join("spec", "fixtures") to locate your fixture directory.
In any other case, you have to manually set the fixture path via:

Savon::Spec::Fixture.path = File.expand_path("../fixtures", __FILE__)

Directory names inside the fixtures directory map to SOAP actions and contain actual SOAP responses from your service(s).
You can use one of those fixtures for the HTTP response body like in the following example:

savon.expects(:get_user).with(:id => 1).returns(:single_user)

As you can see, Savon::Spec uses the name of your SOAP action and the Symbol passed to the #returns method to navigate inside
your fixtures directory and load the requested XML files.

Never

Savon::Spec can also verify that a certain SOAP request was not executed:

savon.expects(:get_user).never

RSpec

This library is optimized to work with RSpec, but it could be tweaked to work with any other testing library.
Savon::Spec installs an after filter to clear out its Savon hooks after each example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.