Last active
December 13, 2015 22:49
-
-
Save themusicman/4987458 to your computer and use it in GitHub Desktop.
spec_helper for Padrino that adds support for assigns(:model_name) in controller specs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV) | |
require File.expand_path(File.dirname(__FILE__) + "/../config/boot") | |
module Padrino | |
module RspecHelpers | |
def self.registered(app) | |
app.set :delivery_method, :test | |
app.extend ApplicationMethods | |
app.after do | |
app._reset_assigns_vars | |
instance_variables.each do |var| | |
app._set_assigns_var(var, instance_variable_get(var)) | |
end | |
end | |
end | |
module ApplicationMethods | |
def _reset_assigns_vars | |
@_assigns_var = {} | |
end | |
def _set_assigns_var(key, value = nil) | |
(@_assigns_var ||= {})[key] = value | |
end | |
def _get_assigns_var(key) | |
(@_assigns_var ||= {}).fetch("@#{key}".to_sym, key) | |
end | |
end | |
module Helpers | |
def assigns(key) | |
app._get_assigns_var(key) | |
end | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.include Rack::Test::Methods | |
config.include Padrino::RspecHelpers::Helpers | |
end | |
def app | |
## | |
# You can handle all padrino applications using instead: | |
# Padrino.application | |
ApplicationName.tap do |app| | |
app.register Padrino::RspecHelpers | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment