[rspec-users] How to use RSpec 2 with Sinatra 1.0

David Chelimsky dchelimsky at gmail.com
Sun Sep 19 14:22:11 EDT 2010


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

> 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:in `rack_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:in `rack_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:in `block (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


More information about the rspec-users mailing list