Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial version

  • Loading branch information...
commit 126b9907f31dba8f1851acb40c4c1e4e83db660a 0 parents
@rubiii rubiii authored
9 .gitignore
@@ -0,0 +1,9 @@
+.DS_Store
+.yardoc
+doc
+coverage
+tmp
+*~
+*.gem
+.bundle
+Gemfile.lock
1  .rspec
@@ -0,0 +1 @@
+--color
2  Gemfile
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
114 README.md
@@ -0,0 +1,114 @@
+Savon::Spec
+===========
+
+Savon testing library
+
+Installation
+------------
+
+Savon::Spec is available through [Rubygems](http://rubygems.org/gems/savon_spec) and can be installed via:
+
+ $ gem install savon_spec
+
+Dependencies
+------------
+
+Currently, the dependencies are very strict. Savon::Spec is meant to be used with:
+
+* [Savon](http://rubygems.org/gems/savon) ~> 0.8.0.beta.3
+* [RSpec](http://rubygems.org/gems/rspec) ~> 2.0.0
+* [Mocha](http://rubygems.org/gems/mocha) ~> 0.9.8
+
+Note to self: the required versions for RSpec and Mocha could probably be lower.
+
+Getting started
+---------------
+
+### Macros
+
+Include the `Savon::Spec::Macros` module:
+
+ RSpec.configure do |config|
+ config.include Savon::Spec::Macros
+ end
+
+### Mock
+
+By including the macros, you have access to the `savon` method in your specs. It returns a `Savon::Spec::Mock` instance to set up your expectations. It's based on Mocha and comes with similiar methods:
+
+ #expects(soap_action) # mocks SOAP request to a given SOAP action
+ #stubs(soap_action) # stubs SOAP requests to a given SOAP action
+ #with(soap_body) # expects Savon to send a given SOAP body
+ #returns(response) # returns the given response
+
+### Fixtures
+
+Savon::Spec works best 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 the command `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__)
+
+The directories inside the fixture directory should map to SOAP actions and the XML fixtures inside those directories should describe the SOAP response. Please take a look at the following examples to better understand this convention.
+
+Some examples
+-------------
+
+ # user.rb
+
+ class User
+
+ def self.all
+ response = client.request :get_all_users
+ response.to_hash.map { |user_hash| new user_hash }
+ end
+
+ def self.find(user_id)
+ response = client.request :get_user do
+ soap.body = { :id => user_id }
+ end
+
+ new response.to_hash
+ end
+
+ end
+
+ # user_spec.rb
+
+ describe User do
+
+ describe ".all" do
+ before do
+ savon.expects(:get_all_users).returns(:multiple_users)
+ end
+
+ it "should return an Array of Users" do
+ User.all.each { |user| user.should be_a(User) }
+ end
+
+ it "should return exactly 7 Users" do
+ User.all.should have(7).items
+ end
+ end
+
+ describe ".find" do
+ before do
+ savon.expects(:get_user).with(:id => 1).returns(:single_user)
+ end
+
+ it "should return a User for a given :id" do
+ User.find(1).should be_a(User)
+ end
+ end
+
+ end
39 Rakefile
@@ -0,0 +1,39 @@
+require "rake"
+
+begin
+ require "yard"
+
+ YARD::Rake::YardocTask.new do |t|
+ t.files = ["README.md", "lib/**/*.rb"]
+ end
+rescue LoadError
+ desc message = %{"gem install yard" to generate documentation}
+ task("yard") { abort message }
+end
+
+begin
+ require "metric_fu"
+
+ MetricFu::Configuration.run do |c|
+ c.metrics = [:churn, :flog, :flay, :reek, :roodi, :saikuro] # :rcov seems to be broken
+ c.graphs = [:flog, :flay, :reek, :roodi]
+ c.flay = { :dirs_to_flay => ["lib"], :minimum_score => 20 }
+ c.rcov[:rcov_opts] << "-Ilib -Ispec"
+ end
+rescue LoadError
+ desc message = %{"gem install metric_fu" to generate metrics}
+ task("metrics:all") { abort message }
+end
+
+begin
+ require "rspec/core/rake_task"
+
+ RSpec::Core::RakeTask.new do |t|
+ t.rspec_opts = %w(-fd -c)
+ end
+rescue LoadError
+ desc message = %{"gem install rspec" to run the specs}
+ task(:spec) { abort message }
+end
+
+task :default => :spec
1  autotest/discover.rb
@@ -0,0 +1 @@
+Autotest.add_discovery { "rspec2" }
10 lib/savon/spec.rb
@@ -0,0 +1,10 @@
+require "savon"
+require "rspec"
+require "mocha"
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
+
+require "savon/spec/version"
+require "savon/spec/macros"
42 lib/savon/spec/fixture.rb
@@ -0,0 +1,42 @@
+module Savon
+ module Spec
+
+ # = Savon::Spec::Fixture
+ #
+ # Loads SOAP response fixtures.
+ class Fixture
+ class << self
+
+ def path
+ @path ||= Rails.root.join("spec", "fixtures").to_s if defined? Rails
+
+ raise ArgumentError, "Savon::Spec::Fixture.path needs to be specified" unless @path
+ @path
+ end
+
+ attr_writer :path
+
+ def load(*args)
+ file = args.map { |arg| arg.to_s.snakecase }.join("/")
+ fixtures[file] ||= load_file file
+ end
+
+ alias [] load
+
+ private
+
+ def fixtures
+ @fixtures ||= {}
+ end
+
+ def load_file(file)
+ full_path = File.expand_path File.join(path, "#{file}.xml")
+ raise ArgumentError, "Unable to load: #{full_path}" unless File.exist? full_path
+
+ File.read full_path
+ end
+
+ end
+ end
+ end
+end
17 lib/savon/spec/macros.rb
@@ -0,0 +1,17 @@
+require "savon/spec/mock"
+
+module Savon
+ module Spec
+
+ # = Savon::Spec::Macros
+ #
+ # Include this module into your RSpec tests to mock/stub Savon SOAP requests.
+ module Macros
+
+ def savon
+ Savon::Spec::Mock.new
+ end
+
+ end
+ end
+end
67 lib/savon/spec/mock.rb
@@ -0,0 +1,67 @@
+require "savon/spec/fixture"
+
+module Savon
+ module Spec
+
+ # = Savon::Spec::Mock
+ #
+ # Mocks/stubs SOAP requests executed by Savon.
+ class Mock
+
+ # Mocks SOAP requests to a given <tt>soap_action</tt>.
+ def expects(soap_action)
+ setup :expects, soap_action
+ self
+ end
+
+ # Stubs SOAP requests to a given <tt>soap_action</tt>.
+ def stubs(soap_action)
+ setup :stubs, soap_action
+ self
+ end
+
+ # Expects a given SOAP body Hash to be used.
+ def with(soap_body)
+ Savon::SOAP::XML.any_instance.expects(:body=).with(soap_body) if mock_method == :expects
+ self
+ end
+
+ # Sets up HTTPI to return a given +response+.
+ def returns(response = nil)
+ http = { :code => 200, :headers => {}, :body => "" }
+
+ case response
+ when Symbol then http[:body] = Fixture[soap_action, response]
+ when Hash then http.merge! response
+ when String then http[:body] = response
+ end
+
+ httpi_mock.returns HTTPI::Response.new(http[:code], http[:headers], http[:body])
+ self
+ end
+
+ private
+
+ def setup(mock_method, soap_action)
+ self.mock_method = mock_method
+ self.soap_action = soap_action
+ self.httpi_mock = new_httpi_mock
+ end
+
+ attr_accessor :mock_method
+
+ def soap_action=(soap_action)
+ @soap_action = soap_action.kind_of?(Symbol) ? soap_action.to_s.lower_camelcase : soap_action
+ end
+
+ attr_reader :soap_action
+
+ def new_httpi_mock
+ HTTPI.send(mock_method, :post).with { |http| http.body.include? soap_action }
+ end
+
+ attr_accessor :httpi_mock
+
+ end
+ end
+end
7 lib/savon/spec/version.rb
@@ -0,0 +1,7 @@
+module Savon
+ module Spec
+
+ VERSION = "0.1.0"
+
+ end
+end
1  lib/savon_spec.rb
@@ -0,0 +1 @@
+require "savon/spec"
26 savon_spec.gemspec
@@ -0,0 +1,26 @@
+lib = File.expand_path("../lib/", __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require "savon/spec/version"
+
+Gem::Specification.new do |s|
+ s.name = "savon_spec"
+ s.version = Savon::Spec::VERSION
+ s.authors = "Daniel Harrington"
+ s.email = "me@rubiii.com"
+ s.homepage = "http://github.com/rubiii/#{s.name}"
+ s.summary = "Savon testing library"
+ s.description = "Test helpers for the Savon SOAP library."
+
+ s.rubyforge_project = s.name
+
+ s.add_dependency "savon", "~> 0.8.0.beta.3"
+ s.add_dependency "rspec", "~> 2.0.0"
+ s.add_dependency "mocha", "~> 0.9.8"
+
+ s.add_development_dependency "httpclient", "~> 2.1.5"
+ s.add_development_dependency "webmock", "~> 1.4.0"
+
+ s.files = `git ls-files`.split("\n")
+ s.require_path = "lib"
+end
10 spec/fixtures/get_user/success.xml
@@ -0,0 +1,10 @@
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
+ <soap:Body>
+ <ns2:getUserResponse xmlns:ns2="http://users.example.com/">
+ <return>
+ <name>Kent</name>
+ <email>kent@example.com</email>
+ </return>
+ </ns2:getUserResponse>
+ </soap:Body>
+</soap:Envelope>
45 spec/savon/spec/fixture_spec.rb
@@ -0,0 +1,45 @@
+require "spec_helper"
+
+describe Savon::Spec::Fixture do
+
+ describe ".path" do
+ it "should return a specified path" do
+ Savon::Spec::Fixture.path = "/Users/rubiii/my_app/spec/fixtures"
+ Savon::Spec::Fixture.path.should == "/Users/rubiii/my_app/spec/fixtures"
+
+ Savon::Spec::Fixture.path = nil # reset to default
+ end
+
+ it "should raise an ArgumentError if accessed before specified" do
+ lambda { Savon::Spec::Fixture.path }.should raise_error(ArgumentError)
+ end
+
+ it "should default to spec/fixtures if used in a Rails app" do
+ Rails = Class.new
+ Rails.expects(:root).returns(Pathname.new("/Users/rubiii/another_app"))
+
+ Savon::Spec::Fixture.path.should == "/Users/rubiii/another_app/spec/fixtures"
+
+ Object.send(:remove_const, "Rails")
+ end
+ end
+
+ describe ".load" do
+ around do |example|
+ Savon::Spec::Fixture.path = "spec/fixtures"
+ example.run
+ Savon::Spec::Fixture.path = nil # reset to default
+ end
+
+ it "should return a fixture for the given arguments" do
+ fixture = Savon::Spec::Fixture.load :get_user, :success
+ fixture.should == File.read("spec/fixtures/get_user/success.xml")
+ end
+
+ it "should memoize the fixtures" do
+ Savon::Spec::Fixture.load(:get_user, :success).
+ should equal(Savon::Spec::Fixture.load(:get_user, :success))
+ end
+ end
+
+end
12 spec/savon/spec/macros_spec.rb
@@ -0,0 +1,12 @@
+require "spec_helper"
+
+describe Savon::Spec::Macros do
+ include Savon::Spec::Macros
+
+ describe "#savon" do
+ it "should return a Savon::Spec::Mock instance" do
+ savon.should be_a(Savon::Spec::Mock)
+ end
+ end
+
+end
166 spec/savon/spec/mock_spec.rb
@@ -0,0 +1,166 @@
+require "spec_helper"
+
+Savon.configure do |config|
+ config.log = false
+end
+
+describe Savon::Spec::Mock do
+ include Savon::Spec::Macros
+
+ let :client do
+ Savon::Client.new do
+ wsdl.endpoint = "http://example.com"
+ wsdl.namespace = "http://users.example.com"
+ end
+ end
+
+ describe "#expects" do
+ before { savon.expects(:get_user).returns }
+
+ it "should set up HTTPI to mock POST requests for a given SOAP action" do
+ client.request :get_user
+ end
+
+ context "without a request to mock" do
+ around do |example|
+ begin
+ example.run
+ rescue Mocha::ExpectationError => e
+ e.message.should include("expected exactly once, not yet invoked: HTTPI.post")
+ end
+ end
+
+ it "should fail" do
+ end
+ end
+ end
+
+ describe "#expects and #with" do
+ before { savon.expects(:get_user).with(:id => 1).returns }
+
+ it "should expect Savon to send a given SOAP body" do
+ client.request :get_user do
+ soap.body = { :id => 1 }
+ end
+ end
+
+ context "with a request without SOAP body" do
+ around do |example|
+ begin
+ example.run
+ rescue Mocha::ExpectationError => e
+ e.message.should include("expected exactly once, not yet invoked: #<AnyInstance:Savon::SOAP::XML>.body=(:id => 1)")
+ end
+ end
+
+ it "should fail" do
+ client.request :get_user
+ end
+ end
+ end
+
+ describe "#stubs" do
+ before { savon.stubs(:get_user).returns }
+
+ it "should set up HTTPI to stub POST requests for a given SOAP action" do
+ client.request :get_user
+ end
+
+ it "should not complain about requests not being executed" do
+ # no request
+ end
+ end
+
+ describe "#stubs and #with" do
+ before { savon.stubs(:get_user).with(:id => 1).returns }
+
+ it "should not expect Savon to send a given SOAP body" do
+ client.request :get_user
+ end
+ end
+
+ describe "#returns" do
+ context "without arguments" do
+ let(:response) { client.request :get_user }
+
+ before { savon.expects(:get_user).returns }
+
+ it "should return a response code of 200" do
+ response.http.code.should == 200
+ end
+
+ it "should not return any response headers" do
+ response.http.headers.should == {}
+ end
+
+ it "should return an empty response body" do
+ response.http.body.should == ""
+ end
+ end
+
+ context "with a String" do
+ let(:response) { client.request :get_user }
+
+ before { savon.expects(:get_user).returns("<soap>response</soap>") }
+
+ it "should return a response code of 200" do
+ response.http.code.should == 200
+ end
+
+ it "should not return any response headers" do
+ response.http.headers.should == {}
+ end
+
+ it "should return the given response body" do
+ response.http.body.should == "<soap>response</soap>"
+ end
+ end
+
+ context "with a Symbol" do
+ let(:response) { client.request :get_user }
+
+ around do |example|
+ Savon::Spec::Fixture.path = "spec/fixtures"
+ savon.expects(:get_user).returns(:success)
+
+ example.run
+
+ Savon::Spec::Fixture.path = nil # reset to default
+ end
+
+ it "should return a response code of 200" do
+ response.http.code.should == 200
+ end
+
+ it "should not return any response headers" do
+ response.http.headers.should == {}
+ end
+
+ it "should return the :success fixture for the :get_user action" do
+ response.http.body.should == File.read("spec/fixtures/get_user/success.xml")
+ end
+ end
+
+ context "with a Hash" do
+ let(:response) { client.request :get_user }
+
+ before do
+ @hash = { :code => 201, :headers => { "Set-Cookie" => "ID=1; Max-Age=3600;" }, :body => "<with>cookie</with>" }
+ savon.expects(:get_user).returns(@hash)
+ end
+
+ it "should return the given response code" do
+ response.http.code.should == @hash[:code]
+ end
+
+ it "should return the given response headers" do
+ response.http.headers.should == @hash[:headers]
+ end
+
+ it "should return the given response body" do
+ response.http.body.should == @hash[:body]
+ end
+ end
+ end
+
+end
4 spec/spec_helper.rb
@@ -0,0 +1,4 @@
+require "bundler"
+Bundler.require(:default, :development)
+
+require "savon_spec"
Please sign in to comment.
Something went wrong with that request. Please try again.