Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Return 400 error on unsuccessful operation

  • Loading branch information...
commit 199aae781e1aac159c6f0bc0e000c7af8f43db48 1 parent 503a338
Bantik authored
View
4 Gemfile
@@ -3,13 +3,15 @@ source "http://rubygems.org"
# Example:
gem "activesupport", ">= 2.3.5"
gem "activemodel"
+gem "rails"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
-group :development do
+group :development, :test do
gem "shoulda", ">= 0"
gem "rdoc", "~> 3.12"
gem "rspec"
+ gem "rspec-rails"
gem "bundler"
gem "jeweler", "~> 1.8.4"
gem "simplecov"
View
70 Gemfile.lock
@@ -1,23 +1,76 @@
GEM
remote: http://rubygems.org/
specs:
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
activemodel (3.2.8)
activesupport (= 3.2.8)
builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
+ arel (3.0.2)
builder (3.0.3)
diff-lcs (1.1.3)
+ erubis (2.7.0)
git (1.2.5)
+ hike (1.2.1)
i18n (0.6.1)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
rdoc
+ journey (1.0.4)
json (1.7.5)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
multi_json (1.3.6)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.2)
+ rack (>= 1.0)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
+ bundler (~> 1.0)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
@@ -29,6 +82,11 @@ GEM
rspec-expectations (2.11.2)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)
+ rspec-rails (2.11.4)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec (~> 2.11.0)
shoulda (3.1.1)
shoulda-context (~> 1.0)
shoulda-matchers (~> 1.2)
@@ -39,6 +97,16 @@ GEM
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.12)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.35)
PLATFORMS
ruby
@@ -48,7 +116,9 @@ DEPENDENCIES
activesupport (>= 2.3.5)
bundler
jeweler (~> 1.8.4)
+ rails
rdoc (~> 3.12)
rspec
+ rspec-rails
shoulda
simplecov
View
2  lib/faceted/controller.rb
@@ -9,7 +9,7 @@ def render_response(obj)
success: obj.success,
response: obj.to_hash,
errors: obj.errors
- }
+ }, :status => obj.success ? 200 : 400
end
# For rendering a response with a multiple objects, e.g.
View
57 spec/controller_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+class Birthplace # Mock AR model
+ attr_accessor :id, :city, :state
+ def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
+ def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
+ def reload; self; end
+end
+
+module MyApi
+
+ class MyApi::Application < Rails::Application
+ end
+
+ class Birthplace
+ include Faceted::Presenter
+ presents :birthplace
+ field :city
+ field :state
+ end
+
+ class BirthplacesController < ActionController::Base
+ include Faceted::Controller
+ include Rails.application.routes.url_helpers
+ def show
+ @birthplace = MyApi::Birthplace.first
+ render_response @birthplace
+ end
+
+ end
+
+end
+
+describe MyApi::BirthplacesController, :type => :controller do
+
+ before do
+ MyApi::Birthplace.stub(:first) { MyApi::Birthplace.new }
+ MyApi::Application.routes.draw do
+ namespace :my_api do
+ resources :birthplaces
+ end
+ end
+ end
+
+ it 'renders with a 200 when the operation is successful' do
+ MyApi::Birthplace.any_instance.stub(:success) { true }
+ get :show, :id => 1
+ response.code.should == "200"
+ end
+
+ it 'renders with a 400 when the operation is unsuccessful' do
+ MyApi::Birthplace.any_instance.stub(:success) { false }
+ get :show, :id => 1
+ response.code.should == "400"
+ end
+
+end
View
3  spec/spec_helper.rb
@@ -3,3 +3,6 @@
require 'active_support'
require 'bundler/setup'
require 'faceted'
+require 'rails/all'
+require 'rspec'
+require 'rspec/rails'
Please sign in to comment.
Something went wrong with that request. Please try again.