2012-02-08 37 views
2

试图设置水豚/ rspec测试简单的sinatra应用程序,但不断得到一个机架错误。与水豚/ sinatra测试机架错误...似乎没有得到Sinatra应用程序通过

hello.rb的

require 'sinatra' 

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

    run! if app_file == $0 
end 

规格/ 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 

注意,我也尝试过

def setup 
    Capybara.app = App.new 
    end 

它生产了同样的错误。

错误:

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/

有和无束的exec运行同样的错误。有任何想法吗?

的Gemfile:

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

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

回答

6

希望这是别人试图建立从西纳特拉文档测试非常有用。

改变

def setup 
    Capybara.app = App 
end 

Capybara.app = App 

解决了这个问题。

+0

可能指定'spec_helper.rb'是一个放置它的好地方。 – 2012-02-29 00:02:33

+0

太棒了!谢谢! FWIW,我的应用程序设置如下:'def app Sinatra ::应用程序 结束 Capybara.app = app' – 2012-11-12 17:00:24

相关问题