Skip to content

Commit

Permalink
Use WebMock for HTTP request expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
rlivsey committed Jul 21, 2011
1 parent 187bcf9 commit d147f3d
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 372 deletions.
5 changes: 3 additions & 2 deletions Rakefile
Expand Up @@ -31,7 +31,7 @@ spec = Gem::Specification.new do |s|
s.rdoc_options = %w(--main README.rdoc)

# Add any extra files to include in the gem
s.files = %w(LICENSE Rakefile README.rdoc rspreedly.gemspec VERSION) + Dir.glob("{spec,lib}/**/*")
s.files = %w(LICENSE Rakefile README.rdoc rspreedly.gemspec) + Dir.glob("{spec,lib}/**/*")
s.require_paths = ["lib"]

# If you want to depend on other gems, add them here, along with any
Expand All @@ -40,14 +40,15 @@ spec = Gem::Specification.new do |s|

# If your tests use any gems, include them here
s.add_development_dependency("rspec")
s.add_development_dependency("webmock")
end

# This task actually builds the gem. We also regenerate a static
# .gemspec file, which is useful if something (i.e. GitHub) will
# be automatically building a gem for this project. If you're not
# using GitHub, edit as appropriate.
#
# To publish your gem online, install the 'gemcutter' gem; Read more
# To publish your gem online, install the 'gemcutter' gem; Read more
# about that here: http://gemcutter.org/pages/gem_docs
Gem::PackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
Expand Down
45 changes: 23 additions & 22 deletions spec/base_spec.rb
Expand Up @@ -8,13 +8,14 @@ class TestAPI < RSpreedly::Base

describe ".api_request" do
it "should raise AccessDenied if a 401 is received" do
stub_http_with_code(401)
stub_request(:put, spreedly_url("/")).to_return(:status => 401)

lambda{
RSpreedly::Base.api_request(:put, '/')
}.should raise_error(RSpreedly::Error::AccessDenied)
end
end

describe "attributes=" do
before(:each) do
@api = TestAPI.new
Expand All @@ -24,62 +25,62 @@ class TestAPI < RSpreedly::Base
@api.attributes = {:something => "test"}
@api.something.should == "test"
end

it "should not fail if an attribute does not exist" do
lambda{
@api.attributes = {:foo => true}
}.should_not raise_error
end
end

describe "error messages" do

before(:each) do
@api = TestAPI.new
end

def do_request
@api.api_request(:post, "/")
end

def failing_request
lambda{
do_request
}.should raise_error
end

it "should not set any errors on a successful request" do
stub_http_with_code(200)
stub_request(:post, spreedly_url("/")).to_return(:status => 200)
do_request
@api.errors.should be_empty
end

it "should set one error in the error array if a string is return " do
stub_http_with_fixture("error_string.txt", 422)
stub_request(:post, spreedly_url("/")).to_return(:body => fixture("error_string.txt"), :status => 422)
failing_request
@api.errors.should == ["Some error back from the response as a string"]
end

it "should set one error in the error array if an xml error with one item is returned" do
stub_http_with_fixture("error.xml", 422)
stub_request(:post, spreedly_url("/")).to_return(:body => fixture("error.xml"), :status => 422)
failing_request
@api.errors.should == ["Email can't be blank"]
@api.errors.should == ["Email can't be blank"]
end

it "should set multiple errors in the error array if an xml error with multiple items is returned" do
stub_http_with_fixture("errors.xml", 422)
stub_request(:post, spreedly_url("/")).to_return(:body => fixture("errors.xml"), :status => 422)
failing_request
@api.errors.should == ["Email can't be blank", "Name can't be blank"]
end
@api.errors.should == ["Email can't be blank", "Name can't be blank"]
end

it "should reset errors on each call" do
# failing one first, which will generate some errors
stub_http_with_fixture("errors.xml", 422)
stub_request(:post, spreedly_url("/")).to_return(:body => fixture("errors.xml"), :status => 422)
failing_request
@api.errors.should_not be_empty

# now a successful one should clear the errors
stub_http_with_code(200)
stub_request(:post, spreedly_url("/")).to_return(:status => 200)
do_request
@api.errors.should be_empty
end
Expand Down
100 changes: 50 additions & 50 deletions spec/config_spec.rb
Expand Up @@ -2,143 +2,143 @@

describe RSpreedly::Config do

before :each do
before :each do

RSpreedly::Config.clear
RSpreedly::Config.setup do |c|
c[:one] = 1
c[:two] = 2
end

end

describe "logger" do
before :each do

before :each do
Object.send(:remove_const, :RAILS_DEFAULT_LOGGER) if defined?(RAILS_DEFAULT_LOGGER)
end

it "should default to RAILS_DEFAULT_LOGGER if defined" do
RAILS_DEFAULT_LOGGER = "something"
RSpreedly::Config.reset
RSpreedly::Config.logger.should == "something"
end

it "should default to a Logger if RAILS_DEFAULT_LOGGER is not defined" do
RSpreedly::Config.reset
RSpreedly::Config.reset
RSpreedly::Config.logger.should be_a(Logger)
end

end

describe "configuration" do

it "should return the configuration hash" do
RSpreedly::Config.configuration.should == {:one => 1, :two => 2}
end

end

describe "[]" do

it "should return the config option matching the key" do
RSpreedly::Config[:one].should == 1
end

it "should return nil if the key doesn't exist" do
RSpreedly::Config[:monkey].should be_nil
end

end

describe "[]=" do

it "should set the config option for the key" do
lambda{
RSpreedly::Config[:banana] = :yellow
}.should change(RSpreedly::Config, :banana).from(nil).to(:yellow)
RSpreedly::Config[:banana] = :yellow
}.should change(RSpreedly::Config, :banana).from(nil).to(:yellow)
end

end

describe "delete" do

it "should delete the config option for the key" do
lambda{
RSpreedly::Config.delete(:one)
}.should change(RSpreedly::Config, :one).from(1).to(nil)
}.should change(RSpreedly::Config, :one).from(1).to(nil)
end

it "should leave the config the same if the key doesn't exist" do
lambda{
RSpreedly::Config.delete(:test)
RSpreedly::Config.delete(:test)
}.should_not change(RSpreedly::Config, :configuration)
end

end

describe "fetch" do

it "should return the config option matching the key if it exists" do
RSpreedly::Config.fetch(:one, 100).should == 1
end

it "should return the config default if the key doesn't exist" do
RSpreedly::Config.fetch(:other, 100).should == 100
RSpreedly::Config.fetch(:other, 100).should == 100
end

end

describe "to_hash" do

it "should return a hash of the configuration" do
RSpreedly::Config.to_hash.should == {:one => 1, :two => 2}
RSpreedly::Config.to_hash.should == {:one => 1, :two => 2}
end

end

describe "setup" do

it "should yield self" do
RSpreedly::Config.setup do |c|
c.should == RSpreedly::Config
end
end

it "should let you set items on the configuration object as a hash" do
lambda{
RSpreedly::Config.setup do |c|
c[:bananas] = 100
end
}.should change(RSpreedly::Config, :bananas).from(nil).to(100)
}.should change(RSpreedly::Config, :bananas).from(nil).to(100)
end

it "should let you set items on the configuration object as a method" do
lambda{
RSpreedly::Config.setup do |c|
c.monkeys = 100
end
}.should change(RSpreedly::Config, :monkeys).from(nil).to(100)
end
}.should change(RSpreedly::Config, :monkeys).from(nil).to(100)
end

end

describe "calling a missing method" do

it "should retreive the config if the method matches a key" do
RSpreedly::Config.one.should == 1
end

it "should retreive nil if the method doesn't match a key" do
RSpreedly::Config.moo.should be_nil
end

it "should set the value of the config item matching the method name if it's an assignment" do
lambda{
RSpreedly::Config.trees = 3
}.should change(RSpreedly::Config, :trees).from(nil).to(3)
end
}.should change(RSpreedly::Config, :trees).from(nil).to(3)
end

end

end

0 comments on commit d147f3d

Please sign in to comment.