Rspec - Ramaze/ramaze GitHub Wiki
Setup
For ruby 1.9, rspec, rdoc, rack-test, bundler using the :test mode
Gemfile
source 'http://rubygems.org'
gem 'bundler' #, '~> 1.0.18'
gem 'ramaze'
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem 'rake'
gem 'rspec' #, '~> 2.6.0'
gem 'bundler' #, '~> 1.0.18'
gem 'simplecov' #, '>= 0'
gem 'rdoc' #, '~> 3.9.4'
end
Rakefile
# encoding: utf-8
APP_NAME='the-greatest-app-ever'
# use bundler to make sure all the dependencies are met for both the app and the development environment.
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
# notice that the bunder setup had to be before bringing in rake
require 'rake'
# standard rspec tasks. Assumes specs will be under the ./spec directory and are named *_spec.rb
require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
# set the default task to run the rspecs
task :default => :spec
# Plain Jane rdoc task except will try to read ./VERSION file that should have
# only one line and that line should contain the application's version.
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION').strip : ""
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "#{APP_NAME} #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
spec_helper.rb
# ruby 1.9 code coverage
require 'simplecov'
SimpleCov.start
# using bundler
require "rubygems"
require "bundler/setup"
# spec dependencies
require "rack"
require "rack/test"
require "rspec"
require 'json'
require 'ramaze'
# using the :test mode
ENV['Ramaze.options.mode'] = :test.to_s
# bring in your ramaze app
require_relative('../app')
Ramaze::Log.level = Logger::DEBUG
RSpec.configure do |config|
config.mock_with :rspec
config.include Rack::Test::Methods
def app
Ramaze.setup_dependencies
Rack::Lint.new(Ramaze.middleware)
end
end
example_spec.rb using rack-test
require 'spec_helper'
describe("Test RESTfulController") do
before :all do
Ramaze::Current.session = Rack::Test::Session.new(Rack::MockSession.new(app))
@session = Ramaze::Current.session
end
it "should handle set replace" do
@session.header 'Accept', 'application/json'
resp = @session.put '/test'
resp.status.should == 200
resp.header['Content-Type'].should == 'application/json'
resp_data = JSON.parse(resp.body)
resp_data['action'].should == 'replace-set'
resp_data['args'].should be_empty
resp_data['error'].should be_nil
resp_data['error_backtrace'].should be_nil
end
end