spec/grape_rabl_spec.rb in grape-rabl-0.3.0 vs spec/grape_rabl_spec.rb in grape-rabl-0.3.1

- old
+ new

@@ -29,29 +29,37 @@ e.message.should include "Use Rack::Config to set 'api.tilt.root' in config.ru" end end context 'titl root is setup' do + let(:parsed_response) { JSON.parse(last_response.body) } + before do subject.before { env['api.tilt.root'] = "#{File.dirname(__FILE__)}/views" } end describe 'helpers' do it 'should execute helper' do subject.get('/home', rabl: 'helper') { @user = OpenStruct.new } get '/home' - last_response.body.should == "{\"user\":{\"helper\":\"my_helper\"}}" + parsed_response.should == JSON.parse("{\"user\":{\"helper\":\"my_helper\"}}") end end describe '#render' do before do subject.get('/home', rabl: 'user') do @user = OpenStruct.new(name: 'LTe') render rabl: 'admin' end + subject.get('/admin/:id', rabl: 'user') do + @user = OpenStruct.new(name: 'LTe') + + render rabl: 'admin' if params[:id] == '1' + end + subject.get('/home-detail', rabl: 'user') do @user = OpenStruct.new(name: 'LTe') render rabl: 'admin', locals: { details: 'amazing detail' } end @@ -65,28 +73,43 @@ end end it 'renders template passed as argument to render method' do get('/home') - last_response.body.should == '{"admin":{"name":"LTe"}}' + parsed_response.should == JSON.parse('{"admin":{"name":"LTe"}}') end + it 'renders admin template' do + get('/admin/1') + parsed_response.should == JSON.parse('{"admin":{"name":"LTe"}}') + end + + it 'renders user template' do + get('/admin/2') + parsed_response.should == JSON.parse('{"user":{"name":"LTe","project":null}}') + end + it 'renders template passed as argument to render method with locals' do get('/home-detail') - last_response.body.should == '{"admin":{"name":"LTe","details":"amazing detail"}}' + parsed_response.should == JSON.parse('{"admin":{"name":"LTe","details":"amazing detail"}}') end it 'renders with locals without overriding template' do get('/about-detail') - last_response.body.should == '{"user":{"name":"LTe","details":"just a user","project":null}}' + parsed_response.should == JSON.parse('{"user":{"name":"LTe","details":"just a user","project":null}}') end it 'does not save rabl options after called #render method' do get('/home') get('/about') - last_response.body.should == '{"user":{"name":"LTe","project":null}}' + parsed_response.should == JSON.parse('{"user":{"name":"LTe","project":null}}') end + + it 'does not modify endpoint options' do + get '/home' + expect(last_request.env['api.endpoint'].options[:route_options][:rabl]).to eq 'user' + end end it 'should respond with proper content-type' do subject.get('/home', rabl: 'user') {} get('/home') @@ -106,10 +129,10 @@ @user = OpenStruct.new(name: 'LTe', email: 'email@example.com') @project = OpenStruct.new(name: 'First') end get '/home' - last_response.body.should == '{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}' + parsed_response.should == JSON.parse('{"user":{"name":"LTe","email":"email@example.com","project":{"name":"First"}}}') end end describe 'template cache' do before do