Skip to content

Instantly share code, notes, and snippets.

@wulftone
Created February 27, 2012 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wulftone/1927904 to your computer and use it in GitHub Desktop.
Save wulftone/1927904 to your computer and use it in GitHub Desktop.
Isolated Rabl Specs
# user.json.rabl_spec.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..'))
$: << File.join(APP_ROOT, 'spec/support') # for my spec helpers
$: << File.join(APP_ROOT, 'config/initializers') # for rabl_init.rb
require 'my_spec_mocks'
require 'rabl'
require 'rabl_init'
# We need to fool rabl into thinking we are running rails.
# All rabl wants is the root path to help in finding templates.
# This is necessary for rabl functions like "extends" and "partial"
# that refer to other templates, but obviously this spec knows nothing
# about where they are, so we tell rabl!
class Rails
def self.root
APP_ROOT
end
end
# managed to shave off about 200ms by not requiring 'active_support/core_ext'
# but I needed this:
module JSON; end
describe 'homefront/users/user.json.rabl'
include MySpecMocks # this is just a set of mocks I can use to fake activerecord objects
before do
f = File.open File.join(APP_ROOT,'app/views/homefront/users/user.json.rabl')
@engine = ::Rabl::Engine.new(f.read, { :format => :json })
@user = make_mock_user
@rendered = JSON.parse @engine.render(nil, { object: @user })
end
context 'renders' do
it 'first_name' do
@rendered['first_name'].should eq @user.first_name
end
it 'last_name' do
@rendered['last_name'].should eq @user.last_name
end
it 'email' do
@rendered['email'].should eq @user.email
end
it 'relationships_as_dependent' do
@rendered['relationships_as_dependent'].should be_present
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment