Skip to content

Commit

Permalink
test(api_formatter): add unit test to cover api_formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulRbr committed Aug 19, 2014
1 parent 0b6ceb0 commit f053578
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'rspec'

require File.expand_path '../../lib/yodatra.rb', __FILE__
require File.expand_path '../../lib/yodatra/api_formatter.rb', __FILE__
require File.expand_path '../../lib/yodatra/models_controller.rb', __FILE__
require File.expand_path '../data/ar_models_controller.rb', __FILE__

Expand All @@ -15,6 +16,7 @@ module RSpecMixin
def app
Sinatra.new {
use Yodatra::Base
use Yodatra::ApiFormatter
use Yodatra::ModelsController
use ArModelsController
}
Expand Down
33 changes: 33 additions & 0 deletions spec/unit/api_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require File.expand_path '../../spec_helper.rb', __FILE__

describe Yodatra::ApiFormatter do
let(:app) { proc{[200,{},['Hello, world.']]} }

before do
@stack = Yodatra::ApiFormatter.new(app, &bloc)
@request = Rack::MockRequest.new(@stack)
end

context 'with a unity bloc' do
let(:bloc) { proc{ |s,h,r| [s,h,r] } }

it 'transforms the response with the given block' do
response = @request.get('/')
expect(response.body).to eq 'Hello, world.'
end
end

context 'with an object wrapper bloc' do
let(:bloc) do
proc do |s,h,r|
transformed = r.map{ |rr| {data: rr}.to_json }
[s,h,transformed]
end
end

it 'transforms the response with the given block' do
response = @request.get('/')
expect(JSON.parse(response.body)['data']).to eq 'Hello, world.'
end
end
end

0 comments on commit f053578

Please sign in to comment.