aaandre
aaandre

Reputation: 2512

Rack error with capybara/sinatra test... Doesn't seem to get the Sinatra app passed on

Trying to set up capybara/rspec for testing a simple sinatra app but keep getting a rack error.

hello.rb

require 'sinatra'

class App < Sinatra::Base
  get "/" do
    "hello hello!"
  end

  run! if app_file == $0
end

spec/hello_spec.rb

require File.join(File.dirname(__FILE__), '..', 'hello.rb')

require 'rspec'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'

set :environment, :test

describe 'The Hello App' do

  include Capybara::DSL
  def setup
    Capybara.app = App
  end

  it "says hello when browsing /" do
    visit '/'
    page.should have_content('hello')
  end
end

Note, I also tried

  def setup
    Capybara.app = App.new
  end

Which produced the same error.

Error:

C:\Sites\misc\qrgen>bundle exec rspec
F

Failures:

  1) The Hello App says hello when browsing /
     Failure/Error: visit '/'
     ArgumentError:
       rack-test requires a rack application, but none was given
     # ./spec/hello_spec.rb:18:in `block (2 levels) in <top (required)>'

Finished in 0.374 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/hello_spec.rb:17 # The Hello App says hello when browsing /

Same error running with and without bundle exec. Any ideas?

Gemfile:

gem "sinatra"
gem "sinatra-contrib"
gem "sinatra-flash"
gem "slim"

gem "rspec"
gem "capybara"
gem "rack-test"

Upvotes: 2

Views: 2540

Answers (1)

aaandre
aaandre

Reputation: 2512

Hope this is useful for someone else trying to set up testing from the Sinatra documentation.

Changing

def setup
  Capybara.app = App
end

to

Capybara.app = App

resolved the issue.

Upvotes: 6

Related Questions