Skip to content

Instantly share code, notes, and snippets.

@robotarmy
Created March 23, 2012 23:55
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save robotarmy/2176510 to your computer and use it in GitHub Desktop.
Save robotarmy/2176510 to your computer and use it in GitHub Desktop.
Adding Rake to Sinatra with Rspec
gem 'sinatra'
group :development,:test do
gem 'rspec'
gem 'rack-test'
end
require 'spec_helper'
describe "My Site" do
def app
app_instance = MySite.prepare_instance
end
context "before" do
it "sets @title" do
get "/"
app.instance_variable_get("@title").should match("Thanks to the person that figured out how to get the Sinatra app instance")
end
end
end

Installing Rspec into Sinatra with Rake

in your project directory

  1. Open the Gist GIST with complete instructions and files
  2. Update the Gemfile
  3. Update the Rakefile
  4. Create a .rspec file
  5. Follow the Instructions

Instructions

# run the bundle command
$ bundle
# .rspec file
$ echo "--color" >> .rspec
$ echo "--backtrace" >> .rspec
# .rspec file
$ echo "--color" >> .rspec
$ echo "--backtrace" >> .rspec
# spec directory
$ mkdir spec
$ touch spec/spec_helper.rb
# Update spec/spec_helper.rb to match file in gist.
# mkdir spec/sample
$ touch spec/sample/instance_variable_spec.rb
# update spec/sample/instance_variable_spec.rb to match file in gist
require 'rspec/core'
require 'rspec/core/rake_task'
task :default => :spec
desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec)
require 'rack/test'
# Require your modules here
# this is how i do it:
# require_relative '../sinatra_modules'
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
#https://gist.github.com/1523353
class Sinatra::Base
@@prepared = nil
def self.onion_core
onion = prototype
loop do
onion = onion.instance_variable_get('@app')
return onion if onion.class == self || onion.nil?
end
end
def self.prepare_instance
@@prepared = onion_core
end
# Override
def call(env)
d = @@prepared || dup
@@prepared = nil
d.call!(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment