How to use RSpec 2 with Sinatra 1.0

Hello,

I am trying to use RSpec 2 with a Sinatra 1.0 application, but I have
no idea about whether they are compatible or not. I tried this:

in $SINATRA_APP_ROOT/app.rb:

require ‘sinatra’

set :app_file, FILE
set :root, File.dirname(FILE)
set :config_path, File.join(Sinatra::Application.root, ‘config’)

require File.join(Sinatra::Application.config_path, ‘config.rb’)

Workaround to fix an issue between Sinatra 1.0 and ruby 1.9.2

Skipped when app is not launched directly

enable :run if $0.eql?(FILE)

get ‘/’ do
haml :index
end

in $SINATRA_APP_ROOT/spec_helper.rb:

require ‘db_web_console’
require ‘rack/test’

in $SINATRA_APP_ROOT/sinatra_app/sinatra_app_spec.rb:

require ‘spec_helper’

def app
@app ||= Sinatra::Application
end

describe ‘Sinatra App’ do

include Rack::Test::Methods

it “says Hello” do
get ‘/’
last_response.should be_ok
last_response.body.should == ‘Hello!’
end
end

But trying

rspec spec/

, I get this error:

Failures:

  1. Sinatra App says ciao
    Failure/Error: @app ||= Sinatra::Application
    uninitialized constant
    RSpec::Core::ExampleGroup::Nested_1::Sinatra

    ./spec/db_web_console/db_web_console_spec.rb:4:in `app’

    $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/

test/methods.rb:31:in build_rack_mock_session' # $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/ test/methods.rb:27:inrack_mock_session’
# $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/
test/methods.rb:42:in build_rack_test_session' # $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/ test/methods.rb:38:inrack_test_session’
# $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/
test/methods.rb:46:in current_session' # ./spec/db_web_console/db_web_console_spec.rb:14:inblock (2
levels) in ’

I tried in other ways, but without success… can someone figure out
what can be the problem?

On Sep 19, 2010, at 4:38 AM, Maurizio De Santis wrote:

set :root, File.dirname(FILE)
end
def app
last_response.body.should == ‘Hello!’
Failures:
test/methods.rb:42:in build_rack_test_session' # $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/ test/methods.rb:38:inrack_test_session’
# $HOME/.rvm/gems/ruby-1.9.2-p0/gems/rack-test-0.5.4/lib/rack/
test/methods.rb:46:in current_session' # ./spec/db_web_console/db_web_console_spec.rb:14:inblock (2
levels) in ’

I tried in other ways, but without success… can someone figure out
what can be the problem?

There is no specific support for Sinatra, but Rack::Test gives you the
tools you need.

In this case, the error message is telling you exactly what the problem
is. It can’t find the constant “Sinatra” that is referenced on line 4 of
sinatra_app_spec.rb. Try adding ‘require “sinatra”’ to
spec/spec_helper.rb.

HTH,
David